C# Copy file to folder with permissions

I'm writing a program that, among other things, needs to copy a particular file to a network folder. Since I'm on a company network, the credentials needed to access that folder are the same as my Windows credentials.

The program works if I open the folder in Explorer, provide my username and password, and then run the uploader. It doesn't work without first providing that username and password.

How do I tell System.IO to supply my DefaultNetworkCredentials to the Copy method? Or is there another method I can use to get this done?

string pathToFile = "myfile.csv";
string pathToRemoteFile = "server.domain.tldDocumentsSubfoldermyfile.csv"

System.IO.File.Copy(pathToFile, pathToRemoteFile); // Fails with IOException "can't find network path"

Thanks!

~ Wogan


The error suggests that it is an incorrect path rather than a permissions problem.

Try this:

string pathToRemoteFile = @"server.domain.tldDocumentsSubfoldermyfile.csv"

[The @ is the string literal quoting symbol; without it the backslash is a special character]

链接地址: http://www.djcxy.com/p/6128.html

上一篇: 通过用户名和密码访问域中的目录

下一篇: C#将文件复制到具有权限的文件夹