Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StandardInputEncoding for ProcessStartInfo?


When i am adding service to windows manually by typing in CMD something like this:

"C:\Program Files (x86)\Windows Resource Kits\Tools\instsrv.exe" "some-pl-char-ąźńćńół" "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe"

... everything is good with service name, but when i try do that in c#:

ProcessStartInfo startInfo = new ProcessStartInfo();
Process myprocess = new Process();

startInfo.FileName = "cmd"; 
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;

myprocess.StartInfo = startInfo; 
myprocess.Start();

StreamWriter sw = myprocess.StandardInput;
StreamReader sr = myprocess.StandardOutput;

Thread.Sleep(200);

string command = ...
       ^ "C:\Program Files (x86)\Windows Resource Kits\Tools\instsrv.exe" "some-pl-char-ąźńćńół" "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe"

sw.WriteLine(command);
sw.WriteLine("exit");

Thread.Sleep(200);

sw.Close();
sr.Close();

then name of created service is: some-pl-char-¦č˝Š˝ˇ-

Why there is problem with code page?
There is something like StandardInputEncoding for ProcessStartInfo?
My active code page in CMD (using chcp) is 852. (Polish)

like image 758
revelvice Avatar asked Jun 08 '11 17:06

revelvice


1 Answers

Arguments belongs assigned to the Arguments property and backslashes needs to be escaped by another one. \ -> \\

Updated:

using (var process = new Process())
{
    var encoding = Encoding.GetEncoding(852);

    var psi = new ProcessStartInfo();
    psi.FileName = "cmd";
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;
    psi.StandardOutputEncoding = encoding;

    process.StartInfo = psi;

    process.Start();

    using (var sr = process.StandardOutput)
    using (var sw = new StreamWriter(process.StandardInput.BaseStream, encoding))
    {
        var command = "....";
        sw.WriteLine(command);
        // etc..                   
    }
}
like image 166
ba__friend Avatar answered Sep 20 '22 09:09

ba__friend