C# 中的网络复制命令 - 未找到文件?
我正在尝试将文件复制到映射驱动器上的网络文件夹。我在命令行中测试了 COPY 是否有效,因此我想尝试在 C# 中自动化该过程。
ProcessStartInfo PInfo;
Process P;
PInfo = new ProcessStartInfo("COPY \"" + "c:\\test\\test.txt" + "\" \"" + "w:\\test\\what.txt" + "\"", @"/Z");
PInfo.CreateNoWindow = false; //nowindow
PInfo.UseShellExecute = true; //use shell
P = Process.Start(PInfo);
P.WaitForExit(5000); //give it some time to finish
P.Close();
引发异常:System.ComponentModel.Win32Exception (0x80004005): 系统找不到指定的文件
我缺少什么?我还需要在命令参数中添加其他内容吗?
我尝试过 File.Copy 但它似乎不起作用 (File.Exists("
) 显示 false。
I'm trying to copy a file over to a networked folder on a mapped drive. I tested out COPY in my command line which worked, so I thought I'd try automating the process within C#.
ProcessStartInfo PInfo;
Process P;
PInfo = new ProcessStartInfo("COPY \"" + "c:\\test\\test.txt" + "\" \"" + "w:\\test\\what.txt" + "\"", @"/Z");
PInfo.CreateNoWindow = false; //nowindow
PInfo.UseShellExecute = true; //use shell
P = Process.Start(PInfo);
P.WaitForExit(5000); //give it some time to finish
P.Close();
Raises an exception : System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
What am I missing? Would I have to add anything else to the command parameters?
I've tried File.Copy but it doesn't appear to work (File.Exists("<mappeddriveletter>:\\folder\\file.txt");
) brings up false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这篇 SO 帖子包含一个
运行命令提示符命令
如何正确执行 的示例。您需要使用
/c copy
作为参数来调用cmd.exe
。This SO post contains an example
Run Command Prompt Commands
how to do it right. You need to call
cmd.exe
with/c copy
as a parameter.好吧,从技术角度来说:
copy
本身不是可执行文件,而仅仅是由cmd
解释的命令。因此基本上,您必须将 cmd.exe 作为进程启动,并向其传递一个标志,使其运行复制命令(您还必须供应作为参数)。无论如何,我会站在 Promit 一边,并建议研究
File.Copy
或类似的东西。e:啊,当我发布这篇文章时,错过了你对 Promit 答案的评论。
Well, for the technical bit:
copy
in itself is not an executable, but merely a command interpreted bycmd
. So basically, you'd have to startcmd.exe
as a process, and pass it a flag that makes it run thecopy
command (which you'll also have to supply as a parameter).Anyways, I'd side with Promit and recommend looking into
File.Copy
or something similar.e: Ah, missed your comment on Promit's answer when I posted this.
使用 文件不是更容易吗?复制?
Wouldn't it be a lot easier to use File.Copy ?