please refer below sample code
using System; using System.Diagnostics; using System.ComponentModel; namespace MyProcessSample { /// <summary> /// Shell for the sample. /// </summary> class MyProcess { /// <summary> /// Opens the Internet Explorer application. /// </summary> void OpenApplication(string myFavoritesPath) { // Start Internet Explorer. Defaults to the home page. Process.Start("IExplore.exe"); // Display the contents of the favorites folder in the browser. Process.Start(myFavoritesPath); } /// <summary> /// Opens urls and .html documents using Internet Explorer. /// </summary> void OpenWithArguments() { // url's are not considered documents. They can only be opened // by passing them as arguments. Process.Start("IExplore.exe", "www.northwindtraders.com"); // Start a Web page using a browser associated with .html and .asp files. Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm"); Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp"); } /// <summary> /// Uses the ProcessStartInfo class to start new processes, both in a minimized /// mode. /// </summary> void OpenWithStartInfo() { ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe"); startInfo.WindowStyle = ProcessWindowStyle.Minimized; Process.Start(startInfo); startInfo.Arguments = "www.northwindtraders.com"; Process.Start(startInfo); } static void Main() { // Get the path that stores favorite links. string myFavoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites); MyProcess myProcess = new MyProcess(); myProcess.OpenApplication(myFavoritesPath); myProcess.OpenWithArguments(); myProcess.OpenWithStartInfo(); } } }
这个问题并不难。你可以使用系统内置的shutdown工具。打开一个CMD窗口,然后敲入 shutdown /? 你就可以看到所有的参数了。
当用户在文本框里面输入了数据后,点击按钮,你直接用下面的函数段就可以了。
Process process = new Process();
process.StartInfo.FileName = "shutdown.exe";
process.StartInfo.Arguments = "parameters";
process.start();
这样就实现了你的关机需求。很容易的。
关机的话是这样的
Process.Start("shutdown", "/s /f /t 0");
就是二楼说的这样,通过process来调用外部exe。
Code
public static void ExecCommand(string command, string args)
{
Process process = new Process();
process.StartInfo.FileName = command;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.Arguments = args;
process.Start();
}
用法:
ExecCommand("Shutdown","-s -t 00");
至于倒计时,用Timer就OK