C#:来自变量字符串的外部 EXE 输入/输出管道
目前,我的应用程序接受一个或多个文本文件,将它们解析为另一种文件类型并将它们放在磁盘上。然后,我调用辅助程序(不是我的)将该文本文件处理为第三个程序。
国外的程序基本上做了一件事:
program.exe --输入:foo.txt --输出:bar.txt
我想知道什么...我可以用我的“对象”中的 string[] 替换 foo.txt 和 bar.txt 吗?
我不想从文本文件中提取信息,然后放入新的文本文件中...我想从我拥有的变量中提取信息...到我要创建的另一个变量中。
基本上,我试图通过从一个变量到另一个变量的管道来保存创建两堆文件的过程。我只是不确定该怎么做。
我现在拥有的(在大多数情况下,删除了一些绒毛):
public bool _Out_Schema(string output)
{
clear(output); // clear output directory
// Assign schema name to path
string path = output + '\\' + lf.filename + ".schema";
using (StreamWriter SW = new StreamWriter(path))
{
SW.Write(lf._schema);
}
return true;
}
public bool _Out_UFD(string input, string output)
{
clear(output); // clear output directory
ProcessStartInfo SI = new ProcessStartInfo();
SI.CreateNoWindow = true;
SI.WindowStyle = ProcessWindowStyle.Hidden;
SI.FileName = "ufdschema.exe"; // foreign program
SI.Arguments = String.Format("--ischema={0}\\{2}.schema --oufd={1}\\{2}.ufd", input, output, (FI.Name.Split('.'))[0]);
using (Process P = Process.Start(SI)) { P.WaitForExit(); }
return true;
}
我想将 lf.schema (字符串变量)作为输入......并创建类似 lf.ufd 的东西作为输出
Currently my application takes in a text file/files, parses them into another file type and puts them on disk. I then call a secondary program (not mine) to process THAT text file into a third.
The foreign program basically does one thing:
program.exe --input:foo.txt --output:bar.txt
What I would like to know... could I replace the foo.txt and bar.txt with string[] in my "object".
Instead of pulling the information from the text file, and putting out into a new text file... I want to pull the info from the variable I have... into another variable I would make.
Basically I'm trying to save the process of creating two stacks of files via piping out of one variable into another. I'm just unsure of how to go about it.
What I have now (For the most part, with some fluff removed):
public bool _Out_Schema(string output)
{
clear(output); // clear output directory
// Assign schema name to path
string path = output + '\\' + lf.filename + ".schema";
using (StreamWriter SW = new StreamWriter(path))
{
SW.Write(lf._schema);
}
return true;
}
public bool _Out_UFD(string input, string output)
{
clear(output); // clear output directory
ProcessStartInfo SI = new ProcessStartInfo();
SI.CreateNoWindow = true;
SI.WindowStyle = ProcessWindowStyle.Hidden;
SI.FileName = "ufdschema.exe"; // foreign program
SI.Arguments = String.Format("--ischema={0}\\{2}.schema --oufd={1}\\{2}.ufd", input, output, (FI.Name.Split('.'))[0]);
using (Process P = Process.Start(SI)) { P.WaitForExit(); }
return true;
}
I'd like to take the lf.schema (string variable) as an input... and create something like lf.ufd as an output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果程序没有办法指定“从标准输入读取并写入标准输出”,则无法轻松解决问题。然而,这是可能的,但它需要一些相当低级的东西;基本上你会使用类似 Detours 来挂钩
CreateFile< /code>,并在看到特殊文件名时将其附加到管道。
ReadFile
、WriteFile
和CloseHandle
不需要重定向,因为它们使用管道和文件句柄。这是一个不简单的解决方案,但如果您有 Win32 API 经验,即使您事先不了解 Detours,也可以在几个小时内完成。
If the program doesn't have a way to specify "read from stdin and write to stdout", you can't easily solve your problem. It is possible to do, however, but it will require some pretty lowlevel stuff; basically you'd use something like Detours to hook
CreateFile
, and make it attach to pipes when it sees special filenames.ReadFile
,WriteFile
andCloseHandle
don't need redirection, since they work with pipes as well as file handles.This is a non-trivial solution, but if you have Win32 API experience it's doable within a couple of hours even if you have no prior knowledge of Detours.
如果
program.exe
期望读取和写入磁盘上的文件,并且通过命令行获取这些文件的名称,则您无法执行任何时髦的重定向来实现它。如果应用程序有其他参数,您可以设置告诉它接受输入并将其输出返回到控制台,而不是文件,那么您应该能够执行某些操作。根据您问题中当前的详细信息,您可能不走运。
If
program.exe
is expecting to read and write to files on disk, and is taking the names of those files via its command line you can't do any funky redirection to make it otherwise.If the app has other parameters you can set to tell it to take input piped in and to return its output to the console, rather than a file, then you should be able to do something. Based on the details currently in your question, you're probably out of luck though.