1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
Imports System.Security
Public Class Form1
Public password As SecureString = New SecureString()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.txtUser.Text = System.Environment.UserName
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
StartNewProcessWithAdminCredentials("C:\MeineAnwendung", Me.txtUser.Text)
End Sub
Private Function StartNewProcessWithAdminCredentials(ByVal ExecutablePathAndName As String, _
ByVal sUsername As String, _
Optional ByVal StartArguments As String = "") As System.Diagnostics.Process
Dim newProcessStartUpInfo As System.Diagnostics.ProcessStartInfo
Dim ReturnCode As Boolean = False
Try
newProcessStartUpInfo = New System.Diagnostics.ProcessStartInfo(ExecutablePathAndName, StartArguments)
'XP oder hoeher (Vista = 6)
If System.Environment.OSVersion.Version.Major >= 5 Then
newProcessStartUpInfo.Verb = "runas"
newProcessStartUpInfo.UseShellExecute = False
newProcessStartUpInfo.Password = password
newProcessStartUpInfo.UserName = sUsername
Return System.Diagnostics.Process.Start(newProcessStartUpInfo)
Else
Return System.Diagnostics.Process.Start(newProcessStartUpInfo)
End If
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try
End Function
Private Sub txtPass_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPass.Enter
password.Clear()
End Sub
Private Sub txtPass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPass.KeyPress
Select Case Asc(e.KeyChar)
Case 33 To 126
password.AppendChar(ChrW(Asc(e.KeyChar)))
Case Else
e.Handled = True
End Select
End Sub
End Class
|