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

问题描述

我想用的Process.Start运行从C#外部的问题,但我遇到了权限问题。当我常打开命令提示符(不作为管理员),然后执行我的命令,他们工作得很好,但是当我通过的Process.Start打开命令提示符,我得到一个写错误的目录。 (我不能写在文件test.log中)
如果我运行它通过的Process.Start管理员它工作正常,但我得到的权限弹出。没有人有任何的想法,可以帮助我想出解决办法?谢谢!

下面是code我使用:

 过程PROC =新工艺();
proc.StartInfo.FileName = @cmd.exe的;
proc.StartInfo.Arguments = @/ K乳胶C:\用户\巴蒂尔\文档\ test.tex;
proc.Start();
proc.WaitForExit();
 

解决方案

我不知道是否它试图写一个诊断日志当前工作目录中,您可能没有权限。 (我不知道随便是否会继承工作目录,或者是包含cmd.exe的目录)。我建议你指定使用新工艺的工作目录 ProcessStartInfo.WorkingDirectory

(顺便说一句,我个人觉得更清洁的创建一个新的的ProcessStartInfo 的填充该 - C#对象初始化使这个特别好),然后调用的Process.Start(的ProcessStartInfo)来启动它。否则,它看起来像有已经是一个过程时,有不止是一份呢。只是MHO虽然,无关你正在调查,可能是这个问题。)

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();
解决方案

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

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

08-23 05:14