2016-07-26 100 views
0

我创建一个窗口应用程序,并根据用户选择我创建一个批处理文件在一个目录复制文件到另一个目录文件创建,但它不执行我不知道为什么?请帮忙。如何使用c#执行批处理文件?

在代码我用户进程类来执行批处理文件,但它通过异常在proc.start();
我的代码是:

  Process proc = null; 
      string tempPath = @"C:\Users\jsolanki\Desktop\temp.bat"; 
      StreamWriter sw = new StreamWriter(tempPath); 
      StringBuilder content = new StringBuilder(); 
      foreach (string item in tempList) 
      { 
       content.Append(string.Format("Copy \"{0}\" \"{1}\\\" \r\n", item, destPath)); 

      } 

      sw.WriteLine(content); 
      sw.Close(); 

      string batDir = tempPath; 
      proc = new Process(); 
      proc.StartInfo.WorkingDirectory = batDir; 
      proc.StartInfo.FileName = "temp.bat"; 
      proc.StartInfo.CreateNoWindow = false; 
      proc.Start(); 
      proc.WaitForExit(); 
      MessageBox.Show("Copy is Complete"); 
+0

检查你的'destPath' – andy

+0

现在的问题是解决我想要感谢Mostafiz :) Thanku –

+0

做一个专业,做好事情。 C#程序员不会对批处理文件进行shell操作。这里是MSDN示例https://msdn.microsoft.com/en-us/library/cc148994(v=vs.100).aspx – 2016-07-26 05:39:18

回答

0
string tempPath = @"C:\Users\jsolanki\Desktop\temp.bat"; 
string batDir = tempPath; 
proc.StartInfo.WorkingDirectory = batDir; 

仔细检查您设置工作目录中的文件,而不是一个目录。

也打印异常消息。