使用Visual Studio中的参数运行批处理

时间:2019-05-27 15:47:56

标签: c# visual-studio powershell batch-file

我正在尝试运行带有参数的批处理文件,它应该是动态的,但不能按照我想要的方式工作。

我有一些带有特定目录的代码,我想使其更加动态。我正在从Visual Studio中的textBox检索目录。

String rep_source = textBox12.Text;
String attribute = textBox4.Text;
String rep_destination = textBox5.Text;
String poly_dest = textBox13.Text;
string SPECIFIC_DIRECTORY= textBox7.Text; //I want to use this for my code
string command = @"/c powershell -executionpolicy unrestricted C:\Folder1\Folder2\batchfile.Bat" + " " + rep_source + " " + poly_dest + " " + attribute + " " + rep_destination;
         System.Diagnostics.Process.Start("cmd.exe", command);

代码可以工作,但是当我尝试检索目录时。

 string command = @"/c powershell -executionpolicy unrestricted " +SPECIFIC_DIRECTORY +"\\batchfile.Bat" + " " + rep_source + " " + poly_dest + " " + attribute + " " + rep_destination;

这不会编译并给我我想要的。 我什至无法访问批处理文件本身。

1 个答案:

答案 0 :(得分:0)

尝试一下:

String com = @"/c powershell -executionpolicy unrestricted C:\Folder1\Folder2\batchfile.Bat" + " " + rep_source + " " + poly_dest + " " + attribute + " " + rep_destination;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + com );
ProcessInfo.CreateNoWindow = true;