简体   繁体   中英

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?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM