如何下载并运行一个.exe文件c#
How to download and run a .exe file c#
在您将其标记为重复之前,是的,确实存在这样的问题,我已经查看了所有这些问题,但仍然无法正常工作。我正在尝试在下载和 运行s .exe 文件的功能中编写代码,但它不会下载,运行 或执行任何操作。我什至删除了 try catch 来查找错误或错误代码,但我没有,所以我不知道我哪里出错了,这是我的代码
public test_Configuration()
{
InitializeComponent();
}
Uri uri = new Uri("http://example.com/files/example.exe");
string filename = @"C:\Users\**\AppData\Local\Temp\example.exe";
private void button1_Click(object sender, EventArgs e)
{
try
{
if(File.Exists(filename))
{
File.Delete(filename);
}
else
{
WebClient wc = new WebClient();
wc.DownloadDataAsync(uri, filename);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = 0;
}
}
private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if(e.Error == null)
{
MessageBox.Show("Download complete!, running exe", "Completed!");
Process.Start(filename);
}
else
{
MessageBox.Show("Unable to download exe, please check your connection", "Download failed!");
}
改变DownloadDataAsync
to DownloadFileAsync
.
wc.DownloadFileAsync(uri, filename);
这段代码在更新文件方面帮助了我很多,所以我想我会展示我的想法,希望其他人也有与我类似的要求。
我需要此代码在单击按钮时执行以下操作:
- 从服务器抓取文件并将其存储在本地AppData\Temp。
- 让我的用户了解最新的安装进度(已下载安装程序)。
- 如果下载成功(注意在删除旧文件检查后删除了 else),启动 "daInstaller.exe",同时终止当前 运行 程序。
- 如果所述文件已经存在(即旧 "daIstaller.exe"),请在将新文件复制到 AppData\Temp 之前删除。
不要忘记保持文件名相同,否则您会在 AppData\Temp 文件夹中留下更多垃圾。
private void button1_Click(object sender, EventArgs e)
{
Uri uri = new Uri("http://example.com/files/example.exe");
filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/example.exe");
try
{
if (File.Exists(filename))
{
File.Delete(filename);
}
WebClient wc = new WebClient();
wc.DownloadFileAsync(uri, filename);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = 0;
}
}
private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
Process.Start(filename);
Close();
Application.Exit();
}
else
{
MessageBox.Show("Unable to download exe, please check your connection", "Download failed!");
}
}
在您将其标记为重复之前,是的,确实存在这样的问题,我已经查看了所有这些问题,但仍然无法正常工作。我正在尝试在下载和 运行s .exe 文件的功能中编写代码,但它不会下载,运行 或执行任何操作。我什至删除了 try catch 来查找错误或错误代码,但我没有,所以我不知道我哪里出错了,这是我的代码
public test_Configuration()
{
InitializeComponent();
}
Uri uri = new Uri("http://example.com/files/example.exe");
string filename = @"C:\Users\**\AppData\Local\Temp\example.exe";
private void button1_Click(object sender, EventArgs e)
{
try
{
if(File.Exists(filename))
{
File.Delete(filename);
}
else
{
WebClient wc = new WebClient();
wc.DownloadDataAsync(uri, filename);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = 0;
}
}
private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if(e.Error == null)
{
MessageBox.Show("Download complete!, running exe", "Completed!");
Process.Start(filename);
}
else
{
MessageBox.Show("Unable to download exe, please check your connection", "Download failed!");
}
改变DownloadDataAsync
to DownloadFileAsync
.
wc.DownloadFileAsync(uri, filename);
这段代码在更新文件方面帮助了我很多,所以我想我会展示我的想法,希望其他人也有与我类似的要求。
我需要此代码在单击按钮时执行以下操作:
- 从服务器抓取文件并将其存储在本地AppData\Temp。
- 让我的用户了解最新的安装进度(已下载安装程序)。
- 如果下载成功(注意在删除旧文件检查后删除了 else),启动 "daInstaller.exe",同时终止当前 运行 程序。
- 如果所述文件已经存在(即旧 "daIstaller.exe"),请在将新文件复制到 AppData\Temp 之前删除。
不要忘记保持文件名相同,否则您会在 AppData\Temp 文件夹中留下更多垃圾。
private void button1_Click(object sender, EventArgs e)
{
Uri uri = new Uri("http://example.com/files/example.exe");
filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/example.exe");
try
{
if (File.Exists(filename))
{
File.Delete(filename);
}
WebClient wc = new WebClient();
wc.DownloadFileAsync(uri, filename);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = 0;
}
}
private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
Process.Start(filename);
Close();
Application.Exit();
}
else
{
MessageBox.Show("Unable to download exe, please check your connection", "Download failed!");
}
}