开发者

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" "som开发者_StackOverflowe-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)


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..                   
    }
}


I had a very similar issue. Although I was working with VB.net, it fixed my issue. I couldnt run the command unless this was set.

startInfo.FileName = "cmd.exe /c";  

instead of

startInfo.FileName = "cmd";  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜