在Windows C#应用程序中下载文件
问题描述:
任何人都可以帮助或协助我这个,
我想下载一个Windows C#应用程序中的文件(可能是PDF)。该文件位于应用程序的EXE可用的文件夹中。如何在不使用FTP或webclient等的情况下做到这一点...
先谢谢
Hi,
Can anyone help or assist me for this,
I want download a file(may be a PDF) in a windows C# application. The file is available in a folder where the application's EXE is available. how to do that without the use of FTP or webclient etc...
Thanks in Advance
答
如果这是一个Windows应用程序,并且该文件已经在应用程序exe目录中,则不需要下载它 - EXE可以读取该文件,因此它可以使用 File.Copy [ ^ ]将其复制到一个新的位置,或者将其作为字节读取,并将其写回到它的新文件夹。
为什么你认为这是一个下载?
If this is a windows application, and the file is already in the application exe directory, they you don't need to download it - the EXE can read the file, so it can either use File.Copy[^] to copy it to a new location, or read it as bytes, and write it back to it's new folder.
Why would you think of this as a download?
private void DownloadFile()
{
wc.DownloadProgressChanged += WcOnDownloadProgressChanged;
wc.DownloadFileCompleted += WcOnDownloadFileCompleted;
string downloadUrl = ConfigurationSettings.AppSettings["fileDownloadPath"] ;
wc.DownloadFileAsync(new Uri(downloadUrl), @"C:\\fileName" );
handle.WaitOne(); // wait for the async event to complete
}
private 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 void WcOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
// handle the progres in case of async
//e.ProgressPercentage
}
private void butsync_Click(object sender, EventArgs e)
{
DownloadFile();
}