簡體   English   中英

以管理員身份運行cmd和命令?

[英]To run cmd as administrator along with command?

這是我的代碼:

try
{
    ProcessStartInfo procStartInfo = new ProcessStartInfo(
                                            "cmd.exe", 
                                            "/c " + command);
    procStartInfo.UseShellExecute = true;
    procStartInfo.CreateNoWindow = true;
    procStartInfo.Verb = "runas";
    procStartInfo.Arguments = "/env /user:" + "Administrator" + " cmd" + command;

    ///command contains the command to be executed in cmd
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

我想保留

procStartInfo.UseShellExecute = true 
procStartInfo.RedirectStandardInput = false;

是否可以在不使用process.standardinput情況下執行命令? 我嘗試執行命令我已經傳入參數,但命令沒有執行。

正如@mtijn所說,你有很多事情要做,以后你也會壓倒一切。 你還需要確保你正確地逃避了事情。

假設您要運行以下提升的命令:

dir c:\

首先,如果您只是通過Process.Start()運行此命令,則會立即打開並關閉一個窗口,因為沒有任何東西可以保持窗口打開。 它處理命令並退出。 要保持窗口打開,我們可以將命令包裝在單獨的命令窗口中,並使用/K開關使其保持運行:

cmd /K "dir c:\"

要運行該命令,我們可以像使用runas.exe一樣使用runas.exe ,除了我們需要更多地轉義。 根據幫助文檔( runas /? ),我們傳遞給runas的命令中的任何引號都需要使用反斜杠進行轉義。 不幸的是,使用上面的命令執行此操作會給我們一個雙重反斜杠,這會使cmd解析器混淆,因此也需要進行轉義。 所以上面的命令將最終成為:

cmd /K \"dir c:\\\"

最后,使用您提供的語法,我們可以將所有內容包裝到runas命令中,並將上面的命令括在另一組引號中:

runas /env /user:Administrator "cmd /K \"dir c:\\\""

從命令提示符運行上面的命令,以確保它按預期方式工作。

鑒於所有這些,最終代碼變得更容易組裝:

        //Assuming that we want to run the following command:
        //dir c:\

        //The command that we want to run
        string subCommand = @"dir";

        //The arguments to the command that we want to run
        string subCommandArgs = @"c:\";

        //I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing
        //Note: arguments in the sub command need to have their backslashes escaped which is taken care of below
        string subCommandFinal = @"cmd /K \""" + subCommand.Replace(@"\", @"\\") + " " + subCommandArgs.Replace(@"\", @"\\") + @"\""";

        //Run the runas command directly
        ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");
        procStartInfo.UseShellExecute = true;
        procStartInfo.CreateNoWindow = true;

        //Create our arguments
        string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @"""";
        procStartInfo.Arguments = finalArgs;

        //command contains the command to be executed in cmd
        using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
        {
            proc.StartInfo = procStartInfo;
            proc.Start();
        }

為什么要用參數初始化進程對象,然后再覆蓋那些參數? 和btw:你設置參數的最后一位你將'命令'連接到'cmd',這沒有多大意義,可能是它失敗的地方(看起來你錯過了一個空格)。

此外,您當前正在使用標准命令行,您可能希望使用runas工具 你也可以從命令行調用runas。

另外,為什么要從命令行運行'command'? 為什么不直接從Process.Start啟動它,然后提供管理員權限? 這里有點偽代碼:

Process p = Process.Start(new ProcessStartInfo()
{
    FileName = <your executable>,
    Arguments = <any arguments>,
    UserName = "Administrator",
    Password = <password>,
    UseShellExecute = false,
    WorkingDirectory = <directory of your executable>
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM