Process.Start Permissions Problem

2019-01-20 09:22发布

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

2条回答
一纸荒年 Trace。
2楼-- · 2019-01-20 09:59

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

查看更多
We Are One
3楼-- · 2019-01-20 10:01

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.

查看更多
登录 后发表回答