gpt4 book ai didi

c# - 如何下载并运行.exe文件c#

转载 作者:行者123 更新时间:2023-11-30 23:29:02 24 4
gpt4 key购买 nike

在您将此标记为重复之前,是的,确实存在这样的问题,我已经查看了所有这些问题,但仍然无法正常工作。我正在尝试编写下载并运行 .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!");
}

最佳答案

更改 DownloadDataAsyncDownloadFileAsync .

wc.DownloadFileAsync(uri, filename);

关于c# - 如何下载并运行.exe文件c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35684243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com