sbonkosky
sbonkosky

Reputation: 2557

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). 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:

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. The task is setup with my account, and I checked off Run with Highest Privileges.

Upvotes: 1

Views: 1351

Answers (1)

David Heffernan
David Heffernan

Reputation: 613511

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. Construct this path by dynamically retrieving the directory which holds your executable at runtime. For example by using Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).

So your code might become

p.FileName = Path.Combine(
    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), 
    "7za.exe"
);

Upvotes: 2

Related Questions