0

I tried to disconnect/connect my modem adapter, named as "конект", but it doesn't work, because adapter's name contains Russian letters. How to force it to work? Please help.

Connect("конект", "", "", true);

    public static void Connect(string adapter, string login, string pass, bool discon)
    {
        string cmd = "";
        if (discon)
        {
            cmd = "rasdial " + '"' + adapter + '"' + @" /disconnect";
        }
        else
        {
            cmd = "rasdial " + '"' + adapter + '"' + " " + login + " " + pass;
        }
        Cmd(cmd);
    }
    public static void Cmd(string URL)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe");
        Process p = new Process();
        startInfo.RedirectStandardInput = true;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.CreateNoWindow = true;
        p = Process.Start(startInfo);
        p.StandardInput.WriteLine(URL);
        p.StandardInput.WriteLine(@"EXIT");
        p.WaitForExit();
        p.Close();
    }

[I know that need just rename adapter with English letters and code will work, but I want to know how to force it work with Russian letters]

Halabella
  • 59
  • 3
  • 7

1 Answers1

1
    p.StandardInput.WriteLine(URL);

ProcessStartInfo is missing a StandardInputEncoding property. That makes it likely that a "URL" string that contains Cyrillic characters is going to get mangled if this code runs on machine that doesn't have a cyrillic code page as the default system code page.

You really want to avoid using input redirection here, it just isn't necessary. Use the /c command option for cmd.exe so that you can pass the command line directly:

startInfo.Arguments = "/c " + URL;
p = Process.Start(startInfo);

Fwiw, not necessary to use cmd.exe either, just run rasdial.exe directly.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536