![](/img/trans.png)
[英]Process.Start will not work when invoked from Task Scheduler on server
[英]Exception using Process.Start() and Windows Task Scheduler
我编写了C#控制台应用程序,该应用程序有时试图使用7zip(特别是7za.exe)解压缩文件。 当我手动运行它时,一切运行正常,但是如果我在Task Scheduler中设置任务并运行它,则会抛出此异常:
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
这是我的代码:
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7za.exe"; //http://www.dotnetperls.com/7-zip-examples
p.Arguments = "x " + zipPath + " -y -o" + unzippedPath;
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
7za.exe是我的项目的一部分,“ Copy to Output Directory = Copy Always
。 该任务是使用我的帐户设置的,并且我选中了Run with Highest Privileges
。
看起来您所依赖的工作目录是包含可执行文件的目录。 并不一定是这种情况。 代替
p.FileName = "7za.exe";
指定7za可执行文件的完整路径。 通过动态检索在运行时保存可执行文件的目录来构造此路径。 例如,通过使用Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
。
所以你的代码可能会变成
p.FileName = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"7za.exe"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.