这可能是完全不可能的,但我想知道是否有办法读取控制台已经打印的值.例如,如果控制台打印
你以10米/秒的速度向北行驶
因此Console.WriteLine("You are travelling north at a speed of 10m/s");,有没有办法读取这一行,然后,为了论证,将这个值放在一个字符串中?
基本上我需要的是读取已经输出到控制台的内容,而不是用户输入.有办法吗?
提前致谢.
我正在执行一个C#程序,即来自另一个C#程序的.exe.但.exe在其程序中有一些Console.WriteLine().我想把标准输出到我的C#程序中.
例如,
考虑一个C#可执行文件,即1.exe,还有另一个程序2.cs.
我从2.cs 1.exe打电话.现在控制台从1. exe显示一些输出.但是我希望我的程序中的输出2.cs. 用于向用户显示信息.
可能吗?请帮忙
谢谢Sai sindhu
我需要Console.WriteLine()输出,并追加到字符串。我不能将Main方法更改为仅追加到字符串而不是写入控制台-我需要一种方法从控制台读取所有已写入的行并将其追加到字符串。
目前,我一直在使用FileStream并将控制台输出重定向到文本文件,然后从中读取。
var fs = new FileStream("dataOut.txt", FileMode.Create);
var sw = new StreamWriter(fs);
Console.SetOut(sw);
Console.SetError(sw);
Run Code Online (Sandbox Code Playgroud)
然后Console.WriteLine("whatever")写入文本文件。但是,我不想在文本文件中来回移动。
这样的事情可能吗?我意识到下面的示例没有。
string outString = "";
Console.SetOut(outString);
Console.SetError(outString);
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取 Windows 的激活状态。我有这个代码:
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/C slmgr /xpr";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
string x = "";
while (!proc.HasExited)
{
x += proc.StandardOutput.ReadToEnd();
}
return x;
Run Code Online (Sandbox Code Playgroud)
有些人可能知道,命令“slmgr /xpr”会弹出一个窗口,通知您 Windows 激活状态。
执行此代码,我得到弹出框(并且“x”为空)。我想要的是获取其中的文本(以便它显示在我表单的标签上)。我想知道是否有任何方法可以从出现的弹出窗口中提取文本,在这种情况下,它会类似于“机器已永久激活”。
有什么简单的方法可以实现这一目标吗?