onsdag, mai 10, 2006

How to start a process with a different username in vb.net

Function ConvertToSecureString(ByVal str As String)

Dim password As New SecureString
For Each c As Char In str.ToCharArray

password.AppendChar(c)

Next

Return password

End Function

Sub Main()

dim username as string = "Administrator"

dim password as SecureString = ConvertToSecureString("my password")

dim domain as string = Nothing

dim filename as string = "notepad.exe" ' %SYSTEMROOT%\system32

Try

System.Diagnostics.Process.Start(filename,username, password, domain)

Catch ex As Win32Exception

MessageBox.Show("Wrong username or password.", "Error logging in as administrator", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try

End Sub

lørdag, mai 06, 2006

How to empty the recycle bin

After 6-7 hours with googleing i finally found out how to empty the recycle bin in VB.net using the windows api.

Usage:
Dim Action As New ShellActions
Action.EmptyRecycleBin()

Public Class ShellActions

Shared Function _
SHEmptyRecycleBin(ByVal hWnd As Integer, ByVal pszRootPath As String, _
ByVal dwFlags As Integer) As Integer
End Function

Sub New()
EmptyRecycleBin()
End Sub

Sub EmptyRecycleBin(Optional ByVal rootPath As String = "", _
Optional ByVal noConfirmation As Boolean = True, Optional ByVal NoProgress _
As Boolean = True, Optional ByVal NoSound As Boolean = True)

Const SHERB_NOCONFIRMATION = &H1
Const SHERB_NOPROGRESSUI = &H2
Const SHERB_NOSOUND = &H4

If rootPath.Length > 0 AndAlso rootPath.Substring(1, 2) <> ":\" Then
rootPath = rootPath.Substring(0, 1) & ":\"
End If

Dim flags As Integer = (noConfirmation And SHERB_NOCONFIRMATION) Or _
(NoProgress And SHERB_NOPROGRESSUI) Or (NoSound And SHERB_NOSOUND)
SHEmptyRecycleBin(0, rootPath, flags)

End Sub

End Class

tirsdag, mai 02, 2006

How to validate an IPaddress

Usage:
if(IsValidIPAddress("127.0.0.1") = True) Then
messagebox.show("The ipaddress is valid.")
else
messagebox.show("The ipaddress is invalid. This might be hostname, try to resolve it.")
end if


Public Function IsValidIPAddress(ByVal str As String) As Boolean
Dim r As Regex = New Regex("((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)")
Dim m As Match = r.Match(str)
If m.Success = True Then
Return True
End If
Return False
End Function

torsdag, april 27, 2006

How to sort a listview control in vb.net

Here is an easy way to sort your listview control from ascending to descending.

Paste this little snippet into your vb.net editor:

Private Sub Listview1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles Listview1.ColumnClick

With Files
.Sort()
.Columns(e.Column).ListView.Sort()
If .Sorting = SortOrder.Ascending Then
.Sorting = SortOrder.Descending
Else
.Sorting = SortOrder.Ascending
End If

End With
End Sub