C#调用其他执行档时,一般采用Process类: 提供对本地和远程进程的访问并使之能够启动和停止本地系统进程。

 1 Process p = new Process();    
 2 p.StartInfo.UseShellExecute = false;
 3 p.StartInfo.RedirectStandardOutput = true;
 4 p.StartInfo.FileName = sExePath;
 5 p.StartInfo.CreateNoWindow = true;
 6 p.StartInfo.Arguments = sParam;//参数以空格分隔,如果某个参数为空,可以传””
 7 p.Start();
 8 p.WaitForExit();
 9 //此处可以返回一个字符串,此例是返回压缩成功之后的一个文件路径
10                 
11 string output = p.StandardOutput.ReadToEnd();
12 return output;
View Code

应用举例:也用来连接共享文件夹

 1   /// <summary>
 2     /// 連接共享文件夾
 3     /// </summary>
 4     /// <param name="path">共享文件夾路徑</param>
 5     /// <param name="userName">用戶名</param>
 6     /// <param name="passWord">密碼</param>
 7     /// <returns></returns>
 8     public bool connectState(string path, string userName, string passWord)
 9     {
10         bool Flag = false;
11         Process proc = new Process();
12         try
13         {
14             proc.StartInfo.FileName = "cmd.exe";
15             proc.StartInfo.UseShellExecute = false;
16             proc.StartInfo.RedirectStandardInput = true;
17             proc.StartInfo.RedirectStandardOutput = true;
18             proc.StartInfo.RedirectStandardError = true;
19             proc.StartInfo.CreateNoWindow = true;
20             proc.Start();
21             string dosLine = @"net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES";
22             proc.StandardInput.WriteLine(dosLine);
23             proc.StandardInput.WriteLine("exit");
24             while (!proc.HasExited)
25             {
26                 proc.WaitForExit(1000);
27             }
28             string errormsg = proc.StandardError.ReadToEnd();
29             proc.StandardError.Close();
30             if (string.IsNullOrEmpty(errormsg))
31             {
32                 Flag = true;
33             }
34             else
35             {
36                 throw new Exception(errormsg);
37             }
38         }
39         catch (Exception ex)
40         {
41             throw ex;
42         }
43         finally
44         {
45             proc.Close();
46             proc.Dispose();
47         }
48         return Flag;
49     }
View Code