简体   繁体   中英

C#: Foreign EXE input/output pipe from variable string

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM