[英]Download any file using windows form application with c#
I'll try to download file using this code.But file size is 0 KB.What is the correct and efficiency way to download the file.我将尝试使用此代码下载文件。但文件大小为 0 KB。下载文件的正确和有效方法是什么。
private void DownloadFile()
{
using (WebClient Client = new WebClient())
{
Client.DownloadFileAsync(
new Uri("http://localhost/sn/userSelect.Designer.cs", UriKind.Absolute),
@"C:\xampp\htdocs\sn\test1.txt");
Client.Dispose();
}
}
Any one can give me the method for download the file over windows form program in C#.thanks谁能给我一个C#windows窗体程序下载文件的方法谢谢
class Program
{
private static WebClient wc = new WebClient();
private static ManualResetEvent handle = new ManualResetEvent(true);
private static void Main(string[] args)
{
wc.DownloadProgressChanged += WcOnDownloadProgressChanged;
wc.DownloadFileCompleted += WcOnDownloadFileCompleted;
wc.DownloadFileAsync(new Uri(@"http://www.nattyware.com/bin/pixie.exe"), @"C:\\pixie.exe");
handle.WaitOne(); // wait for the async event to complete
}
private static void WcOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
//async download completed successfully
}
handle.Set(); // in both the case let the void main() know that async event had finished so that i can quit
}
private static void WcOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
// handle the progres in case of async
//e.ProgressPercentage
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.