https://www.hdzikao.com

process_cmd.StarExecute = false;是什么意思啊(C#无法获取控制台应用程序的输出!)【process_cmd.starexecute=false】

[导读] 大家好,今天小热关注到一个比较有意思的话题,就是关于UseShellExecute的问题,于是小编就整理了3个相关介绍UseShellExecute的解答,让我们一起看看吧。 文章目录: process_cmd.StarExecute = false;是什

process_cmd.StarExecute = false;是什么意思啊(C#无法获取控制台应用程序的输出!)【process_cmd.starexecute=false】

大家好,今天小热关注到一个比较有意思的话题,就是关于UseShellExecute的问题,于是小编就整理了3个相关介绍UseShellExecute的解答,让我们一起看看吧。

文章目录:

  1. process_cmd.StarExecute = false;是什么意思啊
  2. C#无法获取控制台应用程序的输出!
  3. C# process 执行exe文件 死循环问题

一、process_cmd.StarExecute = false;是什么意思啊

process_cmd.StarExecute = false; 这行代码的意思是禁用了使用 ShellExecute 功能。具体来说:

  • 禁用 ShellExecute:当 UseShellExecute 设置为 false 时,意味着不能使用 ShellExecute 来打开文件。ShellExecute 通常用于打开各种类型的文件,包括可执行文件和非可执行文件。但设置为 false 后,你只能启动可执行文件,并且需要提供完整的文件路径。

  • WorkingDirectory 属性的变化:在 UseShellExecute 为 true 时,WorkingDirectory 属性用于指定可执行文件的位置。若 WorkingDirectory 为空字符串,则默认当前目录为可执行文件所在。然而,当 UseShellExecute 设为 false 时,WorkingDirectory 属性并不用于查找可执行文件,它只在启动的新进程中起作用,且仅在该进程的上下文中有意义。

  • 重定向标准输入和输出:当 UseShellExecute 为 false 时,你还可以重定向进程的标准输入、输出和错误流,这在某些需要与控制台应用程序交互的场景中非常有用。

综上所述,process_cmd.StarExecute = false;意味着你禁用了通过 ShellExecute 打开文件的功能,并且改变了 WorkingDirectory 属性的行为,同时允许重定向标准输入和输出。

二、C#无法获取控制台应用程序的输出!

Process p = new Process();

p.StartInfo.FileName = "cmd.exe";

p.StartInfo.Arguments = "/c " + filepath;

//filepath其他控制台程序的路径

p.StartInfo.UseShellExecute = false;

//重定向标准输入

= false;

//重定向标准输出

p.StartInfo.RedirectStandardOutput = true;

//重定向错误输出

p.StartInfo.RedirectStandardError = false;

//设置不显示窗口

p.StartInfo.CreateNoWindow = true;

try

{

//启动进程

p.Start();

//停止3秒钟

Thread.Sleep(3000);

//如果进程结束

//或者你可以等到结束再获取

if (p.HasExited)

{

//从输出流获取执行结果

strRst = p.StandardOutput.ReadToEnd();

}

else

{

p.Kill();

}

}

catch (Exception ex)

{

strRst = "";

}

finally

{

p.Close();

}

三、C# process 执行exe文件 死循环问题

这样改:

string fileName = this.compiler + problemId + userName + ".exe";

StreamReader sr = File.OpenText(this.judgeIn + problemId + ".in");

string test = sr.ReadToEnd();

sr.Close();

DateTime startt = DateTime.Now;

this.p.StartInfo.CreateNoWindow = false;

this.p.StartInfo.UseShellExecute = false;

this.p.StartInfo.FileName = fileName;

this.p.StartInfo.RedirectStandardError = true;

this.p.StartInfo.RedirectStandardInput = true;

this.p.StartInfo.RedirectStandardOutput = true;

Timer timer = new Timer();

timer.Interval = Convert.ToDouble(time);//设置timer间隔

timer.Enabled = true;

timer.AutoReset = false;

this.p.Start();

timer.Start();

this.p.StandardInput.Write(test + '\n');

// string message = this.process.StandardError.ReadToEnd();

string output = this.p.StandardOutput.ReadToEnd();

this.p.WaitForExit();

TimeSpan ts = new TimeSpan();

ts = startt - DateTime.Now;//此处报错 句柄无效

this.p.Close();

timer.Stop();

return output;

到此,以上就是小编对于UseShellExecute的问题就介绍到这了,希望介绍关于UseShellExecute的3点解答对大家有用。

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

相关文章阅读