4

Im trying to make C# application that uses hunpos tagger. Runing hunpos-tag.exe requires three input arguments: model, inputFile, outputFile

In cmd it would look something like this:

hunpos-tag.exe model <inputFile >outputFile

If I just run it with model it writes something and waits for end command. When i tried using standard redirect I either get an exception (i solved this the code was off by a braceket i just get the or scenario now) or I get the results of running the tagger with just model argument. Here's the code:

string inputFilePath =  path + "\\CopyFolder\\rr";
string pathToExe = path + "\\CopyFolder\\hunpos-tag.exe";

ProcessStartInfo startInfo = new ProcessStartInfo
    {
         FileName = pathToExe,
         UseShellExecute = false,
         RedirectStandardInput = true,
         WorkingDirectory = Directory.GetDirectoryRoot(pathToExe),
         Arguments = path + "\\CopyFolder\\model.hunpos.mte5.defnpout",
    };

try
{
    Process _proc = new Process();
    _proc.StartInfo.FileName = pathToExe;
    _proc.StartInfo.UseShellExecute = false;
    _proc.StartInfo.RedirectStandardInput = true;
    _proc.StartInfo.Arguments = path + "\\CopyFolder\\model.hunpos.mte5.defnpout";
    //Magic goes here
    _proc.Start();

    _proc.WaitForExit();
}
catch (Exception e)
{
    Console.WriteLine(e);
}

Any ideas how can I redirect input before starting my process?

user3816378
  • 93
  • 14
  • What is the exception? – Iswanto San Feb 11 '15 at 17:35
  • I believe you have to put the redirect after you start the process, You can pass multiple initial arguments if that's what you want to use when you start the application. In my experience, the redirect is instantaneous. – user1274820 Feb 11 '15 at 17:36
  • the exception is: System.InvalidOperationException: No process is associated with this object. – user3816378 Feb 11 '15 at 17:42

2 Answers2

1

It is not only required to set RedirectStandardInput to true but also you need to use the input stream to write the text you want:

_proc.StandardInput.WriteLine("The text you want to write");
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
-1

There's no need for that ProcessStartInfo if you're setting the info later on. Just get rid of that. And it seems you are already doing what you want. Just creating the process object doesn't start the process, Process.Start does. Just make a new StreamWriter and pass it Process.StandardInput (I think that's right, it may be something else)

  • When I try to do any changes before I start the proces I get the following: System.InvalidOperationException: StandardIn has not been redirected. at System.Diagnostics.Process.get_StandardInput() at PProcess.Program.Main() in – user3816378 Feb 11 '15 at 17:48
  • Well of course you can't write to it before it's started. You'll just have to make that StreamWriter right after Process.Start() – Alexander Maxwell Feb 11 '15 at 17:54
  • Like I said in text above, for some reason it requires the input to be delivered at the same time as the argument. If I define it after the Process.Start() for some reason it won't budge. – user3816378 Feb 11 '15 at 18:03