user350034
user350034

Reputation:

Copy file to other computer using cmd in C#

I want to copy a picture to homegroup shared folder. I have opened cmd.exe through Start Menu > Run > cmd.exe and typed:

copy C:\pic1.png \\SOMECOMP\Users\SOMEONE\Shared

The picture has been copied well. However, when i try to do the same with C#, like that:

System.Diagnostics.Process.Start(@"cmd.exe", @"/c start copy C:\pic1.png \\SOMECOMP\Users\SOMEONE\Shared");

I get the following message:

Access is denied.

How can i fix this?

P.S. - File.Copy throws the same error. For me, the cmd way looked more promising.

Upvotes: 3

Views: 6509

Answers (2)

djeeg
djeeg

Reputation: 6765

You probably need to map the directory

& "net" "use" "$mapdrive" "$password" "/USER:$username"
& "copy" $src "$dest"
& "net" "use" $mapdrive "/delete"

But yes, if you can, use the System.IO.File namespace to copy the file.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

Why on earth would you use Process.Start to copy files when there's File.Copy?

File.Copy(@"C:\pic1.png", @"\\SOMECOMP\Users\SOMEONE\Shared\pic1.png", true);

As far as the access denied message is concerned you will need to ensure that the account you are executing your program under has write permissions to this UNC share which might not be the case with ASP.NET applications or Windows NT services which run under limited privileges accounts.

Upvotes: 5

Related Questions