我有一个vbscript文件,我想在我的vb.net应用程序中运行。在应用程序中,脚本“必须”使用进程。原因是它实际上是从Windows进程调用的。为了让我手动执行vbscript,我必须右键单击快捷方式并选择“以管理员身份运行”。
如何使用vb.net模拟这个?目前执行工作是因为我用它测试它只创建一个文本文件。此外,我想假设用户在管理员组中,并且不希望他们每次都必须登录,因为它将每分钟执行一次。
我的代码:
Dim foo As New System.Diagnostics.Process
foo.StartInfo.WorkingDirectory = "c:\"
foo.StartInfo.RedirectStandardOutput = True
foo.StartInfo.FileName = "cmd.exe"
foo.StartInfo.Arguments = "%comspec% /C cscript.exe //B //Nologo C:\aaa\test.vbs"
foo.StartInfo.UseShellExecute = False
foo.StartInfo.CreateNoWindow = True
foo.Start()
foo.WaitForExit()
foo.Dispose()
感谢。
答案 0 :(得分:0)
ProcessStartInfo类有两个属性可用于定义将运行脚本的用户名
ProcessStartInfo.UserName
ProcessStartInfo.Password
请从MSDN注意:
The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.
Password属性的类型为SecureString。这个类需要一个特殊的初始化代码,如下所示:
' Of course doing this will render the secure string totally 'insecure'
Dim pass As String = "Password"
Dim passString As SecureString = New SecureString()
For Each c As Char In pass
passString.AppendChar(ch)
Next
所以你的代码可以用这种方式改变
Dim foo As New System.Diagnostics.Process
foo.StartInfo.WorkingDirectory = "c:\"
foo.StartInfo.RedirectStandardOutput = True
foo.StartInfo.FileName = "cmd.exe"
foo.StartInfo.Arguments = "%comspec% /C cscript.exe //B //Nologo C:\aaa\test.vbs"
foo.StartInfo.UseShellExecute = False
foo.StartInfo.CreateNoWindow = True
foo.StartInfo.UserName = "administrator"
foo.StartInfo.Password = passString
foo.Start()
foo.WaitForExit()
foo.Dispose()