我正在构建一个脚本,从URL下载文件,然后执行它,但由于某种原因,当我调试脚本时,它抛出了一个错误:System.ComponentModel。Win32Exception: '尝试启动进程时发生错误"与工作目录";该进程无法访问该文件,因为它正在被另一个进程使用。
下面是我的代码:using System.Net.Http;
using System.IO;
using System.Diagnostics;
String myUserName = Environment.UserName;
var httpClient = new HttpClient();
var responseStream = await httpClient.GetStreamAsync("http://exampleurl.com/examplefile.exe");
using var fileStream = new FileStream("C:/Users/"+myUserName+"/examplefile.exe", FileMode.Create);
responseStream.CopyTo(fileStream);
System.Threading.Thread.Sleep(10000); // sleeping waiting until download completes
string str = @"C:/Users/"+myUserName+"/examplefile.exe";
Process process = new Process();
process.StartInfo.FileName = str;
process.Start();
必须添加fileStream.Close()文件下载后。
我也面临同样的问题。使用下面的方法帮助我解决了这个问题:
Process.Start(new ProcessStartInfo() { FileName = _PathSave, UseShellExecute = true });