简体   繁体   English

使用NetworkCredential进行文件传输

[英]file transfer using NetworkCredential

While transferring the file from one machine to another machine using C#.Net without socket code is as follows: 使用不带套接字代码的C#.Net将文件从一台计算机传输到另一台计算机时,如下所示:

string filePath = "C:\\tags.txt";
try
{
    WebClient client = new WebClient();
    string uName = "myUserName", password ="myPassword";
    NetworkCredential nc = new NetworkCredential(uName, password); 
    Uri addy = new Uri("\\192.168.1.28\\Files\\ tags.txt");
    client.Credentials = nc;
    byte[] arrReturn = client.UploadFile(addy, filePath);
    Console.WriteLine(arrReturn.ToString());
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
} 

"192.168.1.28\\Files\\ tags.txt"-> this is another machine IP and location when I debug the error occured in the Uri Class. “ 192.168.1.28 \\ Files \\ tags.txt”->这是我调试Uri类中发生的错误时的另一台计算机IP和位置。

Error is-> Invalid URI: The format of the URI could not be determined. 错误是-> Invalid URI: The format of the URI could not be determined.

Here I could send the file to target machine if the location is in share. 如果位置共享,我可以在这里将文件发送到目标计算机。
Ii need to send a file to the corresponding location even if the folder is not shared. 即使文件夹未共享,我也需要将文件发送到相应位置。

Is the above code correct code? 以上代码正确吗?

have you tried something like File.Copy() ? 您是否尝试过File.Copy()类的东西? I'm not sure if File.Copy uses Windows credentials. 我不确定File.Copy是否使用Windows凭据。 if it does then this is the easiest way to transfer files between two computers. 如果这样做的话,这是在两台计算机之间传输文件的最简单方法。

client.UploadFile() can also use two string parameters instead of an URI and a string; client.UploadFile()也可以使用两个字符串参数来代替URI和一个字符串。 you could try sending the URI via a string. 您可以尝试通过字符串发送URI。

where you wrote the Uri you need to put 4 slashes in front of the ip 在编写Uri的地方,需要在ip前面加上4个斜杠

//This is unrelated
//System.Text.Encoding.ASCII.GetString(arrReturn)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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