簡體   English   中英

具有多個參數的Shutdown.exe進程無法正常工作

[英]Process of Shutdown.exe with multiple arguments, not working

我想創建一個進程,使用shutdown.exe在給定時間后關閉計算機。

這是我的代碼:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = "–s –f –t " + seconds;
Process.Start(startInfo);

其中seconds是int局部變量,用戶決定。

當我運行我的代碼時沒有任何反應。 但是當我手動進入cmd提示符並鍵入:
shutdown.exe - s -f -t 999
然后Windows將彈出一個彈出窗口並告訴我系統將在16分鍾內關閉。

我認為這是因為多個參數的原因是我的方法中止正在進行的系統關閉工作(我從cmd提示符手動創建了systemshutdown)。 這幾乎是相同的,除了在startInfo.Argument

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = "-a";
Process.Start(startInfo);

快速檢查shutdown.exe的用法消息表明它希望斜杠('/')后面的選項參數不是破折號(' - ')。

更換線:

        startInfo.Arguments = "–s –f –t " + seconds;

附:

        startInfo.Arguments = "/s /f /t " + seconds;

使用C#express 2010在我的盒子上產生一個工作結果。

此外,您可以將程序讀取的標准錯誤和標准錯誤重定向到已啟動的進程中,以便您可以了解運行后發生的情況。 為此,您可以存儲Process對象並等待基礎進程退出,以便您可以檢查一切是否順利。

        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;

        Process p = Process.Start(startInfo);
        string outstring = p.StandardOutput.ReadToEnd();
        string errstring = p.StandardError.ReadToEnd();
        p.WaitForExit();

不幸的是,我無法告訴你為什么命令行版本接受選項上的'破折號'前綴而C#執行版本不接受。 但是,希望你所追求的是一個有效的解決方案。

以下代碼的完整列表:

        int seconds = 100;
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.FileName = "shutdown.exe";
        startInfo.Arguments = "/s /f /t " + seconds;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        Process p = Process.Start(startInfo);
        string outstring = p.StandardOutput.ReadToEnd();
        string errstring = p.StandardError.ReadToEnd();
        p.WaitForExit();

暫無
暫無

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

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