none
The Process not work in C# app. RRS feed

  • Question

  • Hello everyone.

    I use this code to run some cmd commands:

    Process.Start("cmd.exe", "/C vpncli.exe -s < data.dat & vpncli.exe status " +
                    "> C:\\BicoinMiningProject\\BitcoinCloudMiningBypassApp\\BitcoinMiningProject" +
                    "\\BitcoinCloudMiningBypassApp\\bin\\Debug\\VPN_Status.txt & taskkill /F /im notepad.exe");

    this works fine. but I need to use it like this:

    using (var pro = new Process())
                {
                    pro.StartInfo.WorkingDirectory = "C:\\Windows\\System32";
                    pro.StartInfo.FileName = "cmd.exe";
                    pro.StartInfo.CreateNoWindow = false;
                    pro.StartInfo.RedirectStandardInput = true;
                    pro.StartInfo.RedirectStandardOutput = false;
                    pro.StartInfo.UseShellExecute = false;
                    pro.StartInfo.Arguments = "/C vpncli.exe -s < data.dat & vpncli.exe status " +
                        "> C:\\BicoinMiningProject\\BitcoinCloudMiningBypassApp\\BitcoinMiningProject" +
                        "\\BitcoinCloudMiningBypassApp\\bin\\Debug\\VPN_Status.txt";
                    pro.Start();
                    pro.StandardInput.Flush();
                    pro.StandardInput.Close();
                    pro.WaitForExit();
    
                }

    because I need to wait for finishing the process before continue.

    this is not working. the cmd is opened but immediately close before running any command.

    Can anybody help me with this?




    Friday, August 9, 2019 8:41 AM

All replies

  • you use "/C" as parameter, so works as designed:

    "/c Carries out the command specified by String and then stops."
    Friday, August 9, 2019 11:44 AM
  • Are you using Cisco AnyConnect Secure Mobility Client, as in Using AnyConnect VPNCLI via batch file?

    You are redirecting standard input in the program but writing nothing to standard input. The "< data.dat" will cause the command prompt (cmd.exe) to redirect standard input but you are overriding that in the program but writing nothing to it.

    You do not need to execute cmd.exe (you can execute vpncli.exe directly) if you are redirecting standard input in the program. If you use cmd.exe to redirect standard input and/or standard output (in the command) then do not do the redirection in the program.



    Sam Hobbs
    SimpleSamples.Info


    Friday, August 9, 2019 6:56 PM