Process问题

starkingpku 2011-07-20 11:48:58
问题是这样:现有一可执行程序p.exe,其作用是从特定文件A.txt中读取字符串,经过处理产生文件B.txt,B.txt中含有特定字符串。现在main函数中想先生成A.txt中的内容,调用该p.exe执行,再读取B.txt进行后续操作。
代码是这样的:
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "p.exe";
process.StartInfo.WorkingDirectory = path;//path是p.exe梭子啊路径
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.Close();
////////////////// 继续操作.....

但问题是:我想不经过文件,而是从内存中向p.exe输入A.txt中字符串,经过p.exe处理后的结果也在内存中交给main函数,这样可以做到吗?如果不可以,那么怎样判断p.exe是执行结束产生了B.txt之后main函数再继续操作呢?
...全文
123 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
starkingpku 2011-08-05
  • 打赏
  • 举报
回复
你能解释一下这个demo吗?
xvv13 2011-07-21
  • 打赏
  • 举报
回复
p.exe是控制台程序就好办,有UI的就基本上很难!
判断p.exe+a.txt=b.txt 这个只要判断b.txt的路径是否存在b.txt 就可以了(如果是一下子创建并写入b.txt的情况),其他情况的判断语句可以参考 b.txt生成时间 大小,10秒不变化就认为 p计算结束(p会自动退出?)

可能的情况太多,既然p不是自己的程序,你说清楚点大家都好给你出主意
自由建客 2011-07-21
  • 打赏
  • 举报
回复
重定向,前提是 p.exe 支持标准输入输出,
aXen 2011-07-21
  • 打赏
  • 举报
回复
帖一段msdn中的demo给你,关于重定向的:


using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

namespace Process_StandardInput_Sample
{
class StandardInputTest
{
static void Main()
{
Console.WriteLine("Ready to sort one or more text lines...");

// Start the Sort.exe process with redirected input.
// Use the sort command to sort the input text.
Process myProcess = new Process();

myProcess.StartInfo.FileName = "Sort.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;

myProcess.Start();

StreamWriter myStreamWriter = myProcess.StandardInput;

// Prompt the user for input text lines to sort.
// Write each line to the StandardInput stream of
// the sort command.
String inputText;
int numLines = 0;
do
{
Console.WriteLine("Enter a line of text (or press the Enter key to stop):");

inputText = Console.ReadLine();
if (inputText.Length > 0)
{
numLines ++;
myStreamWriter.WriteLine(inputText);
}
} while (inputText.Length != 0);


// Write a report header to the console.
if (numLines > 0)
{
Console.WriteLine(" {0} sorted text line(s) ", numLines);
Console.WriteLine("------------------------");
}
else
{
Console.WriteLine(" No input was sorted");
}

// End the input stream to the sort command.
// When the stream closes, the sort command
// writes the sorted text lines to the
// console.
myStreamWriter.Close();


// Wait for the sort process to write the sorted text lines.
myProcess.WaitForExit();
myProcess.Close();

}
}
}
言多必失 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 starkingliwei 的回复:]
说明,p.exe是fortran生成的程序,应该是控制台的。我自己没法修改p.exe.
[/Quote]

既然不能控制p.exe,还是用监听,看有没有b.txt文件。
starkingpku 2011-07-21
  • 打赏
  • 举报
回复
说明,p.exe是fortran生成的程序,应该是控制台的。我自己没法修改p.exe.
机器人 2011-07-20
  • 打赏
  • 举报
回复
可以做到。ProcessStartInfo.Arguments 可以传递参数。当然 p.exe 的代码也需要修改。

process.Start() 之后用再调用 WaitForExit() 确保p执行完毕。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧