Tag: tutorial

Replacing a string in each user’s profile

This routine replaces a string in the text file user.js.  The file is located in each user’s profile which also contains a random string as part of the path name.

Imports System.Net
Imports System.IO

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 Dim strPath As String = "c:\users"
 Dim arrFolders() As String = System.IO.Directory.GetDirectories(strPath)
 Dim MozPath As String

'----------------------------------------------------------------
 ' get list of profiles
 '----------------------------------------------------------------
 For Each arrFolder As String In arrFolders
 MozPath = arrFolder & "\AppData\Roaming\Mozilla\Firefox\Profiles\"

If My.Computer.FileSystem.DirectoryExists(MozPath) = True Then
 Dim arrGUIDpath() As String = System.IO.Directory.GetDirectories(MozPath)

For Each arrGUIDfolder As String In arrGUIDpath
 If My.Computer.FileSystem.FileExists(arrGUIDfolder & "\user.js") = True Then
 Console.WriteLine("notepad " & arrGUIDfolder & "\user.js")

'----------------------------------------------------------------
 ' 1. rename the user.js file
 ' 2. replace the proxy defintion string tinto user.js
 '----------------------------------------------------------------
 Try
 My.Computer.FileSystem.DeleteFile(arrGUIDfolder & "\user2.js")
 Catch ex As Exception
 Application.DoEvents()
 End Try

My.Computer.FileSystem.CopyFile(arrGUIDfolder & "\user.js", arrGUIDfolder & "\user2.js")

My.Computer.FileSystem.WriteAllText(arrGUIDfolder & "\user.js", _
 My.Computer.FileSystem.ReadAllText(arrGUIDfolder & "\user2.js").Replace("user_pref(""network.proxy.type"", 4);", _
 "user_pref(""network.proxy.type"", 0);"), False)
 End If

'----------------------------------------------------------------
 ' clean up to avoid confusion in the future
 '----------------------------------------------------------------
 Application.DoEvents()

Try
 My.Computer.FileSystem.DeleteFile(arrGUIDfolder & "\user2.js")
 Catch ex As Exception
 Application.DoEvents()
 End Try
 Next
 End If

Next
 End
 End Sub

End Class

SQL Insert if not exists

This example shows how to insert a record in an SQL table only if it does not currently exist.

if not exists
 (SELECT * FROM TEST_Table
 WHERE Center = '8882'
 AND Drive = 'K' AND Path = '\\DEV2\WORKGROUP')
 INSERT INTO TEST_Table (Center, Drive, Path, Comment)
 VALUES ('8882','K','\\DEV2\WORKGROUP','Stage Layout')

SQLite syntax:

INSERT OR REPLACE INTO topics (tag, topic) 
SELECT 'GEN:03', 'THE FALL'
WHERE NOT EXISTS 
 (SELECT * FROM topics WHERE tag = 'GEN:03' AND topic = 'THE FALL');

Authenticate User to Security Group

Imports System.Security.Principal

Public Class Form1
Public id As WindowsIdentity = WindowsIdentity.GetCurrent()
Public User As WindowsPrincipal = New WindowsPrincipal(id)

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim flg As Boolean = User.IsInRole(“Domain\Security Group”)

Select Case flg
Case True
MsgBox(“I am a member of the group”)
Case False
MsgBox(“Not a member”)
End Select

End
End Sub

End Class

Generate Random String

randomstring

 

Imports System.Text
 
Public Class Form1
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim s As String = “ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz”
    Dim r As New Random
    Dim sb As New StringBuilder
 
    For i As Integer = 1 To 8
      Dim idx As Integer = r.Next(0, 61)
      sb.Append(s.Substring(idx, 1))
    Next
 
    TextBox1.Text = sb.ToString
  End Sub
End Class

Read MAC Addresses in VB.NET

The following code is a working example for determining the MAC address of your network card.

Imports System.Net.NetworkInformation

Public Class Form1
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    MessageBox.Show(getMacAddress)
    End
  End Sub

  Function getMacAddress()
    Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
    Return nics(1).GetPhysicalAddress.ToString
  End Function

End Class