1
votes

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.

1
The scheduled task will be running under a different profile that does not see 7za.exe on the path. Or, the working directory is not what you expect. Specifying the full path might be a good move. No idea why you want to run with high privileges. Why did you do that, or was that just random trial and error? - David Heffernan
@DavidHeffernan Yeah, was trying highest privileges to see if it made a difference. So if it's run under a different profile, do I just specify the full path to the 7za.exe and it should then run? - sbonkosky

1 Answers

2
votes

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"
);