Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件路径传递给批处理文件_C#_Batch File_Xcopy - Fatal编程技术网

C# 将文件路径传递给批处理文件

C# 将文件路径传递给批处理文件,c#,batch-file,xcopy,C#,Batch File,Xcopy,我正在调用一个.bat文件来复制一个文件夹。是否仍要将文件名和目标传递到批处理文件中 我的.bat文件 XCOPY %1 %2 pause 我用来调用.bat的代码是: process.Start(@"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat"); 我试过这个密码 process.Start(@"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.

我正在调用一个.bat文件来复制一个文件夹。是否仍要将文件名和目标传递到批处理文件中

我的.bat文件

XCOPY %1 %2
pause
我用来调用.bat的代码是:

process.Start(@"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat");
我试过这个密码

process.Start(@"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat" , copyfrom copyto);
因为我以前用它来关闭我的公司,但它不适用于这个

谢谢

更新

process.StartInfo.FileName = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat";
process.StartInfo.Arguments = copyFrom.ToString();
process.StartInfo.Arguments = copyTo.ToString();
process.Start();
这就是我正在使用的代码,但它不起作用。我从XCOPY屏幕上看到:

所以它看起来不像是占用了完整的文件路径。copyto和copyfrom是包含路径的变量

更新

使用azhrei的代码:

String batch = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat";
String src = @"C:\Tricky File Path\Is Here\test1.txt";
String dst = @"C:\And\Goes Here\test2.txt";  
String batchCmd = String.Format("\"{0}\" \"{1}\" \"{2}\"", batch, src, dst);

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = String.Format("/k \"echo {0}\"", batchCmd);

process.Start();
我得到这个输出:


它实际上并没有复制文件。

您可以使用
参数
属性

Process proce = new Process();
proce.StartInfo.FileName = "yourfile.exe";
proce.StartInfo.Arguments =  ..;
proce.Start();
文章:

将其替换为:

var process = new Process();
process.StartInfo.FileName = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat";
process.StartInfo.Arguments = // my arguments
process.Start();
一个更好的选择是用
System.IO
中的等效调用替换XCOPY.BAT文件正在执行的操作(然后您会得到错误处理)。

传递参数


您正在启动批处理文件-您需要使用
cmd.exe

”环绕每个参数(如果参数有空格,则需要)


如果你的批处理文件真的是xcopies而不是别的,那么你可以用
xcopy.exe
替换
cmd.exe
,并删除
/k+batch
,我在Google中输入了这个:将参数传递给批处理文件我需要编辑我的.bat文件才能使用它。目前只有一行,
xcopy copyfrom copyto
将它改为XCOPY%1%2?谢谢ELSheep0我处理您的问题问题问题是它没有将完整的目录传递给批处理文件。通过测试,我将批处理更改为XCOPY“%1”%2,它似乎将copyFrom路径拆分为不同的参数,因为当我运行它时,它返回了“C:\Documents”和“C:\Documents”“我是否需要编辑.bat文件才能使用此文件。它当前只有一行,
XCOPY copyfrom copyto
我是否应该将其更改为
XCOPY%1%2
?抱歉,这是另一条评论的完整副本,但我只是想确保你看到它。感谢这是
cmd.exe
保持窗口打开的参数,这样您就可以看到操作进行得如何了。或者您可以使用
/c
在命令完成后关闭窗口我已经更新了解决方案来处理文件路径(xcopy.bat和copyfrom和copyto)中的空格,但是src和dest都必须是copyfrom和copyto变量,我将路径传递给它们,因为它们正在更改。所以src=copyfrom和dst=copyto。如果有意义的话。我已经尝试了你的代码,但是批处理告诉我参数的数量无效。它似乎给了我日期和时间?啊,是的,当在
cmd.exe
的参数中传递大量引号时,通常需要在整个部分周围多加一对引号。我编辑了这篇文章来修复这个错误。(我已经测试了代码)
  String batch = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat";
  String src = @"C:\Tricky File Path\Is Here\test1.txt";
  String dst = @"C:\And\Goes Here\test2.txt";  
  String batchCmd = String.Format("\"{0}\" \"{1}\" \"{2}\"", batch, src, dst);

  process.StartInfo.FileName = "cmd.exe";
  process.StartInfo.Arguments = String.Format("/k \"{0}\"", batchCmd);

  process.Start();