2017-07-03 103 views
0

使用2個文本框的內容成功運行DOS命令。我試圖找到一個解決方案的問題是............。如何以管理員身份運行CMD.exe

我怎樣提升權限(管理員)

誰能幫我跑?

私人小組的button1_Click(發送者爲對象,例如作爲EventArgs的)把手Button1.Click 殼牌( 「cmd.exe的/ C」 + TextBox1.Text + TextBox2.Text) 結束子

+0

可能的重複:'我如何強制.NET應用程序以管理員身份運行? https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – Werdna

回答

1

運行的應用程序如使用Shell()的管理員是不可能的(除非目標文件設置爲始終以管理員權限運行)。

您可以使用ProcessStartInfoProcess.Start()以管理員權限運行您的應用程序。但是,如果用戶按下,則Process.Start()會引發異常。

Dim psi As New ProcessStartInfo() ' Initialize ProcessStartInfo (psi) 
    psi.Verb = "runas" ' runas = Run As Administrator 
    psi.FileName = "cmd.exe" ' File or exe to run (this cannot take arguments, use ProcessStartInfo.Arguments instead 
    psi.Arguments = "/c " + TextBox1.Text + TextBox2.Text ' Arguments for the process that you're going to run 
    Try 
     Process.Start(psi) ' Run the process (User is required to press Yes to run the program with Administrator access) 
    Catch 
     MsgBox("User cancelled the operation", 16, "") ' User pressed No 
    End Try 
+0

謝謝!!!!!!!!我已經在我的測試項目中使用了上述內容,並且它完美地工作。 –

+0

@JeffreyRoccaforte:如果此答案解決了您的問題,請通過按帖子左側的勾號/複選標記將其標記爲「已接受」。欲瞭解更多信息,請參閱:[**如何接受答案工作?**](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) –

相關問題