c#使用WebClient下载文件并保存
我有下载文件的代码,它只会替换它c#使用WebClient下载文件并保存,c#,C#,我有下载文件的代码,它只会替换它 WebClient webClient = new WebClient(); { webClient.DownloadFile("http://test.png", "C:\PNG.png") } 我只是想知道,是否可以下载文件,然后保存文件,而不是替换旧文件(在上面的示例中为png.png)。每次都创建一个唯一的名称 WebClient webClient = new WebClient(); { web
WebClient webClient = new WebClient();
{
webClient.DownloadFile("http://test.png", "C:\PNG.png")
}
我只是想知道,是否可以下载文件,然后保存文件,而不是替换旧文件(在上面的示例中为png.png)。每次都创建一个唯一的名称
WebClient webClient = new WebClient();
{
webClient.DownloadFile("http://test.png", string.Format("C:\{0}.png", Guid.NewGuid().ToString()))
}
每次创建一个唯一的名称
WebClient webClient = new WebClient();
{
webClient.DownloadFile("http://test.png", string.Format("C:\{0}.png", Guid.NewGuid().ToString()))
}
虽然斯蒂芬斯的回答是完全正确的,但有时这可能是不愉快的。我想创建一个临时文件名(与Stephen的建议没有太大的不同,但是在一个临时文件夹中-很可能是AppData/Local/Temp),并在下载完成后重命名该文件。这个类演示了这个想法,我还没有验证它是否按预期工作,但是如果它确实可以随意使用这个类的话
class CopyDownloader
{
public string RemoteFileUrl { get; set; }
public string LocalFileName { get; set; }
WebClient webClient = new WebClient();
public CopyDownloader()
{
webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted;
}
public void StartDownload()
{
var tempFileName = Path.GetTempFileName();
webClient.DownloadFile(RemoteFileUrl, tempFileName, tempFileName)
}
private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs asyncCompletedEventArgs)
{
string tempFileName = asyncCompletedEventArgs.UserState as string;
File.Copy(tempFileName, GetUniqueFileName());
}
private string GetUniqueFilename()
{
// Create an unused filename based on your original local filename or the remote filename
}
}
如果要显示进度,可以公开一个事件,该事件在抛出WebClient.DownloadProgressChanged
时发出
class CopyDownloader
{
public event DownloadProgressChangedEventHandler ProgressChanged;
private void WebClientOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs downloadProgressChangedEventArgs)
{
if(ProgressChanged != null)
{
ProgressChanged(this, downloadProgressChangedEventArgs);
}
}
public CopyDownloader()
{
webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted;
webClient.DownloadProgressChanged += WebClientOnDownloadProgressChanged;
}
// ...
}
虽然斯蒂芬斯的回答是完全正确的,但有时这可能是不愉快的。我想创建一个临时文件名(与Stephen的建议没有太大的不同,但是在一个临时文件夹中-很可能是AppData/Local/Temp),并在下载完成后重命名该文件。这个类演示了这个想法,我还没有验证它是否按预期工作,但是如果它确实可以随意使用这个类的话
class CopyDownloader
{
public string RemoteFileUrl { get; set; }
public string LocalFileName { get; set; }
WebClient webClient = new WebClient();
public CopyDownloader()
{
webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted;
}
public void StartDownload()
{
var tempFileName = Path.GetTempFileName();
webClient.DownloadFile(RemoteFileUrl, tempFileName, tempFileName)
}
private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs asyncCompletedEventArgs)
{
string tempFileName = asyncCompletedEventArgs.UserState as string;
File.Copy(tempFileName, GetUniqueFileName());
}
private string GetUniqueFilename()
{
// Create an unused filename based on your original local filename or the remote filename
}
}
如果要显示进度,可以公开一个事件,该事件在抛出WebClient.DownloadProgressChanged
时发出
class CopyDownloader
{
public event DownloadProgressChangedEventHandler ProgressChanged;
private void WebClientOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs downloadProgressChangedEventArgs)
{
if(ProgressChanged != null)
{
ProgressChanged(this, downloadProgressChangedEventArgs);
}
}
public CopyDownloader()
{
webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted;
webClient.DownloadProgressChanged += WebClientOnDownloadProgressChanged;
}
// ...
}
您正在使用相同的名称将其保存到同一位置,该位置将每次替换它。请使用其他名称。在下载之前,请检查它是否存在。如果它不下载它。如果它确实做了一些不同的事情(使用不同的名称),对不起,我忘记了。@MarshallOfSound这种方法可能会给你带来麻烦。只需考虑两个线程对较大的文件使用这种方法。第一个线程看到没有名为like的文件,开始下载,然后第二个线程开始运行,看到没有名为like的文件(但是-因为线程1没有完成下载),然后用这个文件名开始下载。然后在下载开始之前制作一个占位符文件。使用该文件名,但完全为空。您将使用相同的名称将其保存到同一位置,该位置将每次替换该文件名。请使用其他名称。在下载之前,请检查它是否存在。如果它不下载它。如果它确实做了一些不同的事情(使用不同的名称),对不起,我忘记了。@MarshallOfSound这种方法可能会给你带来麻烦。只需考虑两个线程对较大的文件使用这种方法。第一个线程看到没有名为like的文件,开始下载,然后第二个线程开始运行,看到没有名为like的文件(但是-因为线程1没有完成下载),然后用这个文件名开始下载。然后在下载开始之前制作一个占位符文件。使用该文件名,但完全为空