开发者

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开发者_如何学Python.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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜