将uploadfile转换为uploadfileasync c#

Ter*_*rii 0 c# asynchronous winforms

您好我正在尝试通过c#将文件上传到我的Web服务器,我在上传文件时遇到问题我的应用程序冻结,直到文件上传完毕并且我想上传它Async但我似乎无法获取代码在这里工作是有代码和我一直得到的错误.

此代码有效,但冻结了表单.

WebClient wc = new WebClient();
wc.Credentials = new System.Net.NetworkCredential(TxtUsername.Text, TxtPassword.Text);
string Filename = TxtFilename.Text;
string Server = TxtServer.Text + SafeFileName.Text;
wc.UploadFile(Server, Filename);
Run Code Online (Sandbox Code Playgroud)

但是,如果我执行此代码,我会收到错误.

WebClient wc = new WebClient();
wc.Credentials = new System.Net.NetworkCredential(TxtUsername.Text, TxtPassword.Text);
string Filename = TxtFilename.Text;
string Server = TxtServer.Text + SafeFileName.Text;
wc.UploadFileAsync(Server, Filename);
Run Code Online (Sandbox Code Playgroud)

并且在尝试使其成为异步时出现此错误

Error 1 The best overloaded method match for System.Net.WebClient.UploadFileAsync(System.Uri, string)' has some invalid arguments.
Error 2 Argument 1: cannot convert from 'string' to 'System.Uri'
Run Code Online (Sandbox Code Playgroud)

Kam*_*ami 6

改变线

wc.UploadFileAsync(Server, Filename);
Run Code Online (Sandbox Code Playgroud)

wc.UploadFileAsync(new Uri(Server), Filename);
Run Code Online (Sandbox Code Playgroud)

UploadFileAsync不带字符串参数,因此您需要Uri从服务器地址创建一个.有关详细信息,请参阅MSDN文档.