将标准输出重定向到文件问题?
您好,当我尝试执行此过程时,为什么会收到访问被拒绝错误? 我正在使用这个新进程运行 (MSBuild "projectfile here" "additional args") 命令
public bool CmdExecute(string command,string args)
{
bool isOk = true;
try
{
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.EnableRaisingEvents = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = command;
proc.StartInfo.Arguments = args;
// Console.Out.WriteLine(proc.StartInfo.Arguments);
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(output);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
isOk = false;
}
return isOk;
}
Hi why am I getting a access denied error when I try to execute this process?
I am running (MSBuild "projectfile here" "additional args") command with this new process
public bool CmdExecute(string command,string args)
{
bool isOk = true;
try
{
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.EnableRaisingEvents = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = command;
proc.StartInfo.Arguments = args;
// Console.Out.WriteLine(proc.StartInfo.Arguments);
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(output);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
isOk = false;
}
return isOk;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读 Process.StartInfo.RedirectStandardOutput 上的文档。你没有正确使用它。
http://msdn.microsoft.com/en-us /library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
如果您使用 RedirectStandardOutput,您应该阅读 Process.StandardOutput 和 Process.StandardError 并用它做一些事情。
我怀疑您的访问被拒绝,因为您生成的命令行没有意义。您会得到:
无关的与号 (&) 将导致问题。
Read the docs on Process.StartInfo.RedirectStandardOutput. You're not using it correctly.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
If you're using RedirectStandardOutput, you should be reading Process.StandardOutput and Process.StandardError and doing something with it.
I suspect that you're getting an access denied as your resulting command line doesn't make sense. You would get:
The extraneous ampersand (&) is going to cause problems.