0

Building a console app that will execute an exe file(pagesnap.exe). I would like to hide its window(pagesnap.exe) during execution. How is it done.

ProcessStartInfo Psi = new ProcessStartInfo("D:\\PsTools\\");
Psi.FileName = "D:\\PsTools\\psexec.exe";    
Psi.Arguments = @"/C \\DESK123 D:\files\pagesnap.exe";
Psi.UseShellExecute = false;
Psi.RedirectStandardOutput = true;
Psi.RedirectStandardInput = true;
Psi.WindowStyle = ProcessWindowStyle.Hidden;
Psi.CreateNoWindow = true;
Process.Start(Psi).WaitForExit();

DESK123 is the local PC. Would later try this with remote PCs.

Things I have tried

Psi.Arguments = @"/C start /b psexec \\DESK123 D:\files\pagesnap.exe";  
Psi.Arguments = @"/b psexec \\DESK123 D:\files\pagesnap.exe";  
Psi.Arguments = @"/C psexec \\DESK123 /b D:\files\pagesnap.exe"; 
Psi.Arguments = @"/C psexec \\DESK123 D:\files\pagesnap.exe 2>&1 output.log";

Update: I have built pagesnap with Output type as a windows application instead of console. The cmd window doesn't come up, now. Seems this is the only way for me

sukesh
  • 2,379
  • 12
  • 56
  • 111
  • 2
    See this one:http://stackoverflow.com/questions/3440105/hide-command-window-in-c-sharp-application – Amittai Shapira Sep 02 '15 at 07:07
  • @AmittaiShapira I did go through that but do not know where to use `/b`. I tried `@"/b psexec` and also `".. /b D:\files\pagesnap.exe"`. Neither worked. – sukesh Sep 02 '15 at 07:22
  • 1
    *What* are you trying to do? There is no reason to execute `cmd` if you really want to execute `psexec`. Check [this duplicate answer](http://stackoverflow.com/questions/25782308/execute-exe-on-remote-machine) – Panagiotis Kanavos Sep 02 '15 at 07:52
  • @PanagiotisKanavos. I have updated my question. The cmd window doesnt show up only if PageSnap is built as windows app and not a console app. – sukesh Sep 02 '15 at 08:09
  • @Qwery I think you confused what executes where and what is displayed. ProcessInfo controls how you run `psexec`, not `pagesnap. To control how `pagesnap` appears you should pass the appropriate arguments to `psexec`, eg `-d` for non-interactive – Panagiotis Kanavos Sep 02 '15 at 08:16

1 Answers1

2

Simply call the following function. Pass the argument as your command and your working directory

private string BatchCommand(string cmd, string mapD)
    {


        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd);
        procStartInfo.WorkingDirectory = mapD;
        // The following commands are needed to redirect the standard output.
        // This means that it will be redirected to the Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.RedirectStandardInput = true;
        procStartInfo.UseShellExecute = false;
        // Do not create the black window.
        procStartInfo.CreateNoWindow = true;
        // Now we create a process, assign its ProcessStartInfo and start it
        System.Diagnostics.Process cmdProcess = new System.Diagnostics.Process();
        cmdProcess.StartInfo = procStartInfo;
        cmdProcess.ErrorDataReceived += cmd_Error;
        cmdProcess.OutputDataReceived += cmd_DataReceived;
        cmdProcess.EnableRaisingEvents = true;
        cmdProcess.Start();
        cmdProcess.BeginOutputReadLine();
        cmdProcess.BeginErrorReadLine();
        cmdProcess.StandardInput.WriteLine("ping www.google.com");     //Execute ping
        cmdProcess.StandardInput.WriteLine("exit");                  //Execute exit.
        cmdProcess.WaitForExit();

        // Get the output into a string

        return Batchresults;
    }
    static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
            Batchresults += Environment.NewLine + e.Data.ToString();

    }

     void cmd_Error(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
        {
            Batchresults += Environment.NewLine + e.Data.ToString();

        }
    }
amit dayama
  • 3,246
  • 2
  • 17
  • 28
  • Batchresults is a global variable of type string – amit dayama Sep 02 '15 at 07:21
  • I just tried your solution and it still opens the pagesnap's command window – sukesh Sep 02 '15 at 07:29
  • it works for me perfectly. procStartInfo.CreateNoWindow = true; line hides the command window. – amit dayama Sep 02 '15 at 07:31
  • 1
    /C psexec \\DESK123 D:\files\pagesnap.exe you are passing it as argument. instead try using /C psexec \\DESK123 D:\files\pagesnap.exe 2>&1 Logfile.txt as argument It will hide the window and will also write the output to logfile – amit dayama Sep 02 '15 at 07:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88563/discussion-between-amit-dayama-and-qwerty). – amit dayama Sep 02 '15 at 09:41