Monday, April 13, 2009

C#: Executing dos commands without opening the console window

while googling i encountered a superb post at one of the blogs of Microsoft developers, in which it there was a discussion on executing a dos command and reading the output. This interested me a lot.

Ive developed small programs which were simple front end GUI for dos commands. While executing the programs, the dos console always popped up. This was very annoying, i wanted it not to appear and still execute the dos command.

I wanted to read the output of the dos command, execute the command without command window popping up.

The following code is about opening a "cmd" and executing a "ipconfig" command without window showing up from the console. This would be effective while executing the code from a Windows Form rather than.

I beleive that guys with basic .NET programming skill can understand this and PORT it to a windows FORM application.

the code goes as follows....

this code will open a invisible dos command window, execute the command and read the output into a string variable.

i have referred this post, before writing this small article
without dos window, execute dos commands


  1. This was very helpful for something I'm working on. Thanks!

  2. Thanks a lot for this. I wanted a solution for java but i used the same concept and i was able to do it.

  3. Hi,

    I want to open a console (cmd.exe), send some commands and read the output. Unfortunately everything res returns is:

    "Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.


    Do you know how to accomplish this task?