Process.Start 权限问题 [英] Process.Start Permissions Problem

查看:52
本文介绍了Process.Start 权限问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:UsersShaneDocuments	est.tex";
proc.Start();
proc.WaitForExit();

推荐答案

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

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.

(顺便说一句,我个人觉得创建一个新的 ProcessStartInfo 填充它更简洁 - C# 对象初始化程序使这特别好)然后调用 Process.Start(ProcessStartInfo) 启动它.否则,看起来好像已经有一个过程,而实际上还没有一个过程.不过只是 MHO,可能与您正在调查的问题无关.)

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

这篇关于Process.Start 权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆