📅  最后修改于: 2023-12-03 15:21:30.397000             🧑  作者: Mango
本文将介绍如何编写一个 C# 程序,该程序可以下载并运行一个 exe 文件,并将其结果输出到控制台上。我们将使用 Windows Forms 应用程序作为程序的 GUI,并使用 System.Net 命名空间中的 WebClient 类从 URL 下载文件。
下面是本程序的步骤:
private async void button1_Click(object sender, EventArgs e)
{
string url = "http://example.com/example.exe";
string filepath = @"C:\temp\example.exe";
using (WebClient client = new WebClient())
{
await client.DownloadFileTaskAsync(new Uri(url), filepath);
}
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = filepath;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
}
此代码将执行以下操作:
通过本文,我们已经了解了如何编写一个 C# 程序,该程序可以下载并运行一个 exe 文件,并将其输出结果输出到控制台上。