1

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.

MHTri
  • 910
  • 2
  • 10
  • 30
  • `File.Exists("\\server\file.txt")` Sure, that would return false. You need to escape your backslashes, or use an `@`-string: `File.Exists(@"\\server\file.txt")` – Joe White Mar 18 '11 at 16:19

3 Answers3

2

Well, for the technical bit: copy in itself is not an executable, but merely a command interpreted by cmd. So basically, you'd have to start cmd.exe as a process, and pass it a flag that makes it run the copy 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.

ig2r
  • 2,396
  • 1
  • 16
  • 17
  • Ah yes, that makes sense, because the Process is trying to find the program "COPY blah blah blah" instead of "cmd.exe" so it would obviously return file not found, I just didn't interpret it as C# not finding the program rather than the file to be copied. – MHTri Mar 18 '11 at 16:31
2

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.

Community
  • 1
  • 1
Doc Brown
  • 19,739
  • 7
  • 52
  • 88
1

Wouldn't it be a lot easier to use File.Copy ?

Promit
  • 3,497
  • 1
  • 20
  • 29
  • I should've clarified in my question - File.Copy does not appear to work over networked shares... or at least, I can't get it to work. – MHTri Mar 18 '11 at 16:17
  • We've seen some inconsistent behavior with File.Copy if we use the drive's shared mapping, so we use the fully qualified network name instead, this may work better for you. Should be something like \\sharedServer.company.internal\destination – BrMcMullin Mar 18 '11 at 16:42