[英]How to make a Save-file-as dialog box after downloading file
Description: So I have this following script to download a file from the internet with a progress bar showing what percentage the download is at and custom message boxes. 说明:因此,我有以下脚本用于从Internet下载文件,并带有一个进度条,显示下载的百分比以及自定义消息框。 Now I have the files being saved to the users %TEMP% path.
现在,我已将文件保存到用户%TEMP%路径。 And it uses events to prevent people from clicking on the button again and starting a new download.
它使用事件来防止人们再次单击该按钮并开始新的下载。
Problem: I want to give the user a choice of where to save the file, but show his temp path as the default location. 问题:我想让用户选择保存文件的位置,但是将其临时路径显示为默认位置。 (Like a Save-file-dialog Box) I'm still fairly new to coding and don't know where to exactly start.
(就像一个保存文件对话框),我对编码还很陌生,也不知道从哪里开始。
What I tried: I didn't try any new code, but I did go around google and try to find a solution. 我尝试过的事情:我没有尝试任何新代码,但是我确实走遍了Google并试图找到解决方案。 Here are some websites that I found that might be useful:
以下是一些我发现可能有用的网站:
https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-save-files-using-the-savefiledialog-component https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-save-files-using-the-savefiledialog-component
https://www.c-sharpcorner.com/UploadFile/mahesh/savefiledialog-in-C-Sharp/ https://www.c-sharpcorner.com/UploadFile/mahesh/savefiledialog-in-C-Sharp/
They explain it really well. 他们解释得很好。 But I don't know how to incorporate it into this script.
但是我不知道如何将其合并到此脚本中。 And I dont want to write a brand new script.
而且我不想写一个全新的脚本。 Any Help would be Appreciated!
任何帮助,将不胜感激!
private bool _isBusy = false;
private void button1_Click(object sender, EventArgs e)
=> DownloadFile("someurl1", "somefilename1.exe");
private void button2_Click(object sender, EventArgs e)
=> DownloadFile("someurl2", "somefilename2.exe");
private void button3_Click(object sender, EventArgs e)
=> DownloadFile("someurl3", "somefilename3.exe");
private void button4_Click(object sender, EventArgs e)
=> DownloadFile("someurl4", "somefilename4.exe");
private void DownloadFile(string url, string fileName)
{
if(_isBusy) return;
_isBusy = true;
var output = Path.Combine(Path.GetTempPath(), fileName);
MessageBox.Show($"{fileName} will start downloading from {url}");
using (WebClient client = new WebClient())
{
client.DownloadFileCompleted += (sender, args) =>
{
MessageBox.Show($"{fileName} Complete!");
Process.Start(output);
_isBusy = false;
};
client.DownloadProgressChanged += (sender, args) => progressBar1.Value = args.ProgressPercentage;
client.DownloadFileAsync(new Uri(url), output);
}
}
Maybe something like? 也许像?
private SaveFileDialog save = new SaveFileDialog();
private void DownloadFile(string url, string fileName)
{
if (_isBusy) return;
save.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
save.FileName = fileName;
if (save.ShowDialog() == DialogResult.OK)
{
_isBusy = true;
var output = save.FileName;
MessageBox.Show($"{fileName} will start downloading from {url}");
using (WebClient client = new WebClient())
{
client.DownloadFileCompleted += (sender, args) =>
{
MessageBox.Show($"{fileName} Complete!");
Process.Start(output);
_isBusy = false;
};
client.DownloadProgressChanged += (sender, args) => progressBar1.Value = args.ProgressPercentage;
client.DownloadFileAsync(new Uri(url), output);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.