首页 新闻 搜索 专区 学院

我一直在纳闷 跟怎样用WinForm窗体程序来执行DOS指令

0
悬赏园豆:20 [已关闭问题]

最近我在做一个WinForm窗体程序 用它来给计算机定时(在文本框中输入时间值,到时间后系统自动执行关机)

靑椒炒雞蛋的主页 靑椒炒雞蛋 | 初学一级 | 园豆:0
提问于:2009-02-15 13:46
< >
分享
其他回答(4)
0
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();

       		}	
	}
}
Wayne Gao | 园豆:260 (菜鸟二级) | 2009-02-15 14:21
0

这个问题并不难。你可以使用系统内置的shutdown工具。打开一个CMD窗口,然后敲入 shutdown /? 你就可以看到所有的参数了。

当用户在文本框里面输入了数据后,点击按钮,你直接用下面的函数段就可以了。

Process process = new Process();

process.StartInfo.FileName = "shutdown.exe";

process.StartInfo.Arguments = "parameters";

process.start();

这样就实现了你的关机需求。很容易的。

Becool | 园豆:205 (菜鸟二级) | 2009-02-15 17:11
0

关机的话是这样的

Process.Start("shutdown", "/s /f /t 0");

Gray Zhang | 园豆:17610 (专家六级) | 2009-02-15 18:06
0

就是二楼说的这样,通过process来调用外部exe。

Todd Wei | 园豆:170 (初学一级) | 2009-02-15 18:43
0
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
winzheng | 园豆:8797 (大侠五级) | 2009-02-16 11:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册