vb.net – 使用管理员权限运行cmd.exe
这是我的代码尝试使用admin priviligies运行cmd.exe.但是,我得到请求操作需要提升.如果我通过我的Windows运行带有“以管理员身份运行”的cmd.exe,它可以通过vb运行,但事实并非如此.这是我的代码.
Try Dim process As New Process() process.StartInfo.FileName = "cmd.exe " process.StartInfo.Verb = "runas" process.StartInfo.UseShellExecute = False process.StartInfo.RedirectStandardInput = True process.StartInfo.RedirectStandardOutput = True process.StartInfo.RedirectStandardError = True process.StartInfo.CreateNoWindow = True process.Start() process.StandardInput.WriteLine("route add 8.31.99.141 mask 255.255.255.255 " & cmdorder) process.StandardInput.WriteLine("exit") Dim input As String = process.StandardOutput.ReadToEnd process.Close() Dim regex As Regex = New Regex("(ok)+",RegexOptions.IgnoreCase) ' wa requested ' txtLog.AppendText(input) Return regex.IsMatch(input) 谢谢.
你无法实现自己想要的目标.
您可以使用Process.Start()来启动提升的进程,但仅当您使用UseShellExecute = true时: Dim process As New Process() process.StartInfo.FileName = "cmd.exe " process.StartInfo.Verb = "runas" process.StartInfo.UseShellExecute = True process.Start() 原因是,如果要启动提升的进程,则必须使用ShellExecute.只有ShellExecute知道如何提升. 如果指定UseShellExecute = False,则使用CreateProcess而不是ShellExecute. CreateProcess不知道如何提升.为什么? From the AppCompat guy:
这样就解释了如何提升cmd,但是一旦你这样做了:你不允许重定向输出或隐藏窗口; as only
这是一个很长的说法,这家伙问same question over here;但没有让某人关闭你的问题的侮辱.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |