Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't write input to process C# Mono

Tags:

c#

linux

shell

mono

I'm having a issue trying to write input to linux process. Here the code goes:

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.WorkingDirectory = "'/home/"+user+"/pacotes/"+nome_pacote.Text+"-1.0/'";
        process.StartInfo.FileName="dh_make";
        process.StartInfo.Arguments="-n -s";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardInput = true;
        process.Start();
        Thread.Sleep(3000);
        process.StandardInput.WriteLine();

And here goes the error:

System.IO.IOException: Write fault on path /home/vinholi/Ubuntu One/Uso do linux em flash drives/Programa gerador de .deb/GeradorDeb/GeradorDeb/bin/Debug/[Unknown] at System.IO.FileStream.FlushBuffer (System.IO.Stream st) [0x00000] in :0 at System.IO.FileStream.FlushBuffer () [0x00000] in :0 at System.IO.FileStream.Dispose (Boolean disposing) [0x00000] in :0

like image 371
João Vinholi Avatar asked Nov 13 '22 09:11

João Vinholi


1 Answers

One likely explanation for this error is that the process exited before you're attempting to write to it. I tried this with /bin/date and a legitimate StartInfo.WorkingDirectory and the assembly is /Workspace/export/misc/H.exe. This yields:

Unhandled Exception:
System.IO.IOException: Write fault on path /Workspace/export/misc/[Unknown]
  at System.IO.FileStream.WriteInternal (System.Byte[] src, Int32 offset, Int32 count) [0x00097] in /Workspace/mono/mcs/class/corlib/System.IO/FileStream.cs:658 
  at System.IO.FileStream.Write (System.Byte[] array, Int32 offset, Int32 count) [0x000aa] in /Workspace/mono/mcs/class/corlib/System.IO/FileStream.cs:634 
  at System.IO.StreamWriter.FlushBytes () [0x00043] in /Workspace/mono/mcs/class/corlib/System.IO/StreamWriter.cs:222 
  at System.IO.StreamWriter.FlushCore () [0x00012] in /Workspace/mono/mcs/class/corlib/System.IO/StreamWriter.cs:203 
  at System.IO.StreamWriter.Write (System.Char[] buffer) [0x00022] in /Workspace/mono/mcs/class/corlib/System.IO/StreamWriter.cs:351 
  at System.IO.TextWriter.WriteLine () [0x00000] in /Workspace/mono/mcs/class/corlib/System.IO/TextWriter.cs:257 
  at X.Main () [0x00066] in /Workspace/export/misc/H.cs:17 

You are getting this every time when you're using an invalid directory in process.StartInfo.WorkingDirectory. There's nothing that can be done about that, though the error message should be made clearer.

Your directory is invalid because of the quotation marks. You also should not concatenate path names the way you did as it makes your application non-portable to non-Unix operating systems. Instead, write it as:

var home = Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
process.StartInfo.WorkingDirectory = Path.Combine (home, "pacotes", nome_pacote.Text+"-1.0");

Using Environment.GetFolderPath() makes it easier to run your application on a Mac (where home directories are in /Users/<username> instead of /home/<username>).

like image 186
Martin Baulig Avatar answered Nov 14 '22 22:11

Martin Baulig