Process.Start权限问题

发布于 2024-10-03 07:00:03 字数 442 浏览 0 评论 0原文

我尝试使用 Process.Start 从 C# 运行外部问题,但遇到权限问题。当我正常打开命令提示符(不是以管理员身份)并运行命令时,它们工作正常,但是当我通过 Process.Start 打开命令提示符时,我在目录上收到写入错误。 (“我无法在文件 test.log 上写入”)
如果我通过 Process.Start 以管理员身份运行它,它工作正常,但我会弹出权限。有人有任何想法可以帮助我解决这个问题吗?谢谢!

这是我正在使用的代码:

Process proc = new Process();
proc.StartInfo.FileName = @"cmd.exe";
proc.StartInfo.Arguments = @"/k latex C:\Users\Shane\Documents\test.tex";
proc.Start();
proc.WaitForExit();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-10-10 07:00:03

我想知道它是否正在尝试将诊断日志写入当前工作目录,而您可能没有权限。 (我暂时不知道它是否会继承工作目录,或者是包含cmd.exe的目录。)我建议您使用ProcessStartInfo.WorkingDirectory指定新进程的工作目录。

(顺便说一句,我个人发现创建一个新的 ProcessStartInfo 并填充它更干净 - C# 对象初始值设定项使这特别好),然后调用 Process.Start(ProcessStartInfo)开始它。否则,看起来好像已经有了一个流程,但实际上还没有一个流程。不过,这只是 MHO,可能与您正在调查的问题无关。)

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

£噩梦荏苒 2024-10-10 07:00:03

不要使用 cmd.exe 作为 Process 对象的 FileName 属性,而是将命令保存在一个批处理文件中,然后使用该文件执行。

您还可以通过 Process 类的 StartInfo 属性提及管理员的权限,例如用户名、密码、域等。如果你使用这些属性,我认为权限问题就不会出现。 此处您可以找到有关 StartInfo 属性的更多信息。

希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文