Jump to content

A Few Useful VB.NET Snippets/Functions


Recommended Posts

Hi,

these are a few VB.NET snippets/functions that i have found EXTREMELY useful. Originally, i didn't write these myself, i used other people's. But i have since used my own knowledge to replicate them, as the computer with all the original code decided to blue screen repeatedly.

 

Before i continue...

I am not 100% sure what imports need to be made for these functions to work. However, i do know that if you import all of these, they will ALL work.

Imports System.Net
Imports System.Text
Imports System.Web
Imports System.Threading
Imports Microsoft.Win32
Imports System.IO
Imports System.Net.Mail

 

SendData Function-

This function sends postdata, so you can replicate the action of something like a PHP form sending. This can be useful when creating webform bots (to automatically enter information to PHP forms). I use this function a lot for my post to PasteBin function.

 

Private Function SendData(ByVal PostData As String, ByVal Site As String) As String
       Dim Request As HttpWebRequest = DirectCast(WebRequest.Create(Site), HttpWebRequest)
       Request.Method = "POST"
       Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
       Request.ContentType = "application/x-www-form-urlencoded"
       Request.AllowAutoRedirect = True
       Request.ServicePoint.Expect100Continue = False
       Request.ContentLength = PostData.Length
       Using ReqStream As Stream = Request.GetRequestStream()
           Dim Encoding As New UTF8Encoding()
           Dim PostBytes As Byte() = Encoding.GetBytes(PostData)
           ReqStream.Write(PostBytes, 0, PostBytes.Length)
       End Using
       Dim Result As String = String.Empty
       Using Response As HttpWebResponse = Request.GetResponse()
           Using RespStream As Stream = Response.GetResponseStream()
               Using Reader As New StreamReader(RespStream)
                   Result = Reader.ReadToEnd()
               End Using
           End Using
       End Using
       Return Result
   End Function

 

Example of usage:

SendData(TextBox1.Text, "http://example.com/send.php")

This would send the content of TextBox1 as postdata via send.php. Of course, you would need the correct format to send data. There are various FireFox addons which allow you to get the correct format to do so, for example, Live HTTP Headers.

 

Post to PasteBin-

This snippet is 100% my own and uses the SendData function. It simply creates a post on PasteBin.com containing the user's input. This can be very useful when coding, as it allows you to share your work quickly.

 

    Private Function PasteBinPost(ByVal pbsd As String, ByVal datatosend As String)
       Dim tmp As String = System.Web.HttpUtility.UrlEncode(datatosend)
       SendData("submit=submit&paste_parent_key=&paste_subdomain=" & pbsd & "&paste_code=" & tmp & "&paste_format=1&paste_expire_date=N&paste_private=0&paste_name=&paste_email=&submit.x=58&submit.y=17&submit=submit", "http://" & pbsd & ".pastebin.com/post.php")
   End Function

 

Example of usage:

PasteBinPost("testsubdomain", TextBox1.Text)

This would send the content of TextBox1 to the pastebin.com subdomain at http://testsubdomain.pastebin.com. It works well if you create 2 TextBoxes and allow the user to choose the subdomain and post content, or allow them to generate a random subdomain.

 

GmailSender-

This function is my favourite. It allows you to send emails via the Gmail SMTP server. It requires a Gmail account to access the SMTP server, however it is free to create one and has limitless emails. One note is that there is a limit to how fast you can send the emails, but nothing significant. This would only prevent you from spam sending emails, nothing more.

 

    Private Function GmailSender(ByVal GmailUsername As String, ByVal GmailPassword As String, ByVal EmailSubject As String, ByVal EmailBody As String, ByVal EmailTo As String)
       Dim MailSetup As New MailMessage
       MailSetup.Subject = EmailSubject
       MailSetup.To.Add(EmailTo)
       MailSetup.From = New MailAddress(GmailUsername)
       MailSetup.Body = EmailBody
       Dim SMTP As New SmtpClient("smtp.gmail.com")
       SMTP.Port = 587
       SMTP.EnableSsl = True
       SMTP.Credentials = New Net.NetworkCredential(GmailUsername, GmailPassword)
       SMTP.Send(MailSetup)
   End Function

 

Example of usage:

GmailSender("[email protected]", "ExamplePass", "Test Email Subject", "Test Message Body", "[email protected]")

Please note, to my knowledge you can send emails to any email address, not just Gmail. But you need to use Gmail credentials to send emails.

 

 

Using System Tray Icons (NotifyIcon)-

This is a very simple, but extremely useful snippet. It shows you how to use NotifyIcons to create a minimize to tray feature, with alert bubbles. You need to add a NotifyIcon to your form to do this, which can be found in the Common Controls section of the toolbox.

 

    Private Sub MinimizeToTray()
       Me.Hide()
       NotifyIcon1.Visible = True
   End Sub
   Private Sub ShowForm()
       Me.Show()
       NotifyIcon1.Visible = False
   End Sub
   Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
       ShowForm()
   End Sub

These two subs are the minimize/show features. If you want to hide the form and show the tray icon, you would use the MinimizeToTray sub. You reverse this with the ShowForm sub, which is accessed by double clicking the tray icon in this case.

 

For alert bubbles, you use this:

NotifyIcon1.ShowBalloonTip(5000, "Example Bubble Title", "This is an example alert bubble.")

 

This allows you to alert the user of anything necessary while the form is minimized to tray.

 

 

That is all i have for now. I will post any extras that i remember if i do so. I hope these help.

GuidesForScapers.png

 

Legalize baby punching. Tax and regulate it. Punch babies erry day.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.