【发布时间】:2020-04-26 18:21:22
【问题描述】:
我想在 c# 中使用进程运行 exe 文件,当我运行 exe 文件时,输出将是按键上按下的字符的字符串,并将使用 RedirectStandardOutput = true 打印到进程中。
我将收到一个包含文件的byte[] 并将其转换为文件,然后创建一个激活文件的新进程,我的意图是从文件中读取(键盘按下的字符串/字符)和在 TB 中看到它,这是一个 TextBlock(我正在使用 WPF)。
但是当我运行它时,它会打印上面提到的 excaption。
这是我的代码:
private void p1()
{
byte[] b1 = the byte[] containing the file;
FileStream file = File.Create("p.exe");
file.Write(b1, 0, b1.Length);
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = file.Name,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
Thread t = new Thread(() => { res = ReadFromP(process); });
t.Start();
t.Join();
TB.Text=res;
}
catch
{
TB.text="didnt worked";
}
}
private string ReadFromP(Process p)
{
try
{
string line;
p.Start();
// while (!p.StandardOutput.EndOfStream)
//{
line = p.StandardOutput.ReadLine();
//}
return line;
//p.WaitForExit();
}
catch (Exception e)
{
return(e.Message);
}
}
【问题讨论】:
-
写完后你关闭文件了吗?