Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.Start Permissions Problem

I'm trying to run an external problem from C# by using Process.Start, but am running into permissions issues. When I open a command prompt normally (not as an admin) and run my commands they work fine, but when I open a command prompt via Process.Start, I get a write error on the directory. ("I can't write on file test.log")
If I run it as an admin via Process.Start it works fine, but I get the permissions popup. Does anyone have any ideas that might help me figure this out? Thanks!

Here is the code I'm using:

Process proc = new Process();
proc.StartInfo.FileName = @"cmd.exe";
proc.StartInfo.Arguments = @"/k latex C:\Users\Shane\Documents\test.tex";
proc.Start();
proc.WaitForExit();
like image 391
Shane Fulmer Avatar asked Nov 19 '10 07:11

Shane Fulmer


2 Answers

I wonder whether it's trying to write a diagnostic log to the current working directory, which you may not have permissions for. (I don't know offhand whether it will inherit the working directory, or be the directory that contains cmd.exe.) I suggest you specify the working directory for the new process using ProcessStartInfo.WorkingDirectory.

(As an aside, I personally find it cleaner to create a new ProcessStartInfo an populate that - C# object initializers make this particularly nice) and then call Process.Start(ProcessStartInfo) to start it. Otherwise it looks like there's already a process when there isn't really one yet. Just MHO though, and unrelated to the problem you're investigating, probably.)

like image 188
Jon Skeet Avatar answered Oct 14 '22 21:10

Jon Skeet


Instead of using cmd.exe as a FileName property of Process object, keep your commands in one batch file and then use that file for execution.

Also you can mention administrator's privilages like username, password, domain etc via StartInfo property of Process class. If you use these properties I think permission problem will not come. Here you can find more information about StartInfo property.

Hope it helps.

like image 42
Shekhar Avatar answered Oct 14 '22 20:10

Shekhar