C#:来自变量字符串的外部 EXE 输入/输出管道

发布于 2024-09-15 21:01:49 字数 1378 浏览 2 评论 0原文

目前,我的应用程序接受一个或多个文本文件,将它们解析为另一种文件类型并将它们放在磁盘上。然后,我调用辅助程序(不是我的)将该文本文件处理为第三个程序。

国外的程序基本上做了一件事:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2024-09-22 21:01:49

如果程序没有办法指定“从标准输入读取并写入标准输出”,则无法轻松解决问题。然而,这是可能的,但它需要一些相当低级的东西;基本上你会使用类似 Detours 来挂钩 CreateFile< /code>,并在看到特殊文件名时将其附加到管道。 ReadFileWriteFileCloseHandle 不需要重定向,因为它们使用管道和文件句柄。

这是一个不简单的解决方案,但如果您有 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 and CloseHandle 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.

薄荷港 2024-09-22 21:01:49

如果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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文