[英]Exception using Process.Start() and Windows Task Scheduler
I wrote C# console app that at some point tries to unzip a file using 7zip (specifically 7za.exe). 我编写了C#控制台应用程序,该应用程序有时试图使用7zip(特别是7za.exe)解压缩文件。 When I run it manually everything runs fine, but I if I setup a task in Task Scheduler and have it run it throws this exception:
当我手动运行它时,一切运行正常,但是如果我在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)
Here's my code: 这是我的代码:
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 is part of my project, with Copy to Output Directory = Copy Always
. 7za.exe是我的项目的一部分,“
Copy to Output Directory = Copy Always
。 The task is setup with my account, and I checked off Run with Highest Privileges
. 该任务是使用我的帐户设置的,并且我选中了
Run with Highest Privileges
。
It looks like you are relying on the working directory being the directory that contains the executable. 看起来您所依赖的工作目录是包含可执行文件的目录。 And that is not necessarily the case.
并不一定是这种情况。 Instead of
代替
p.FileName = "7za.exe";
specify the full path to the 7za executable. 指定7za可执行文件的完整路径。 Construct this path by dynamically retrieving the directory which holds your executable at runtime.
通过动态检索在运行时保存可执行文件的目录来构造此路径。 For example by using
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
. 例如,通过使用
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
。
So your code might become 所以你的代码可能会变成
p.FileName = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"7za.exe"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.