Posts Tagged ‘Process’

Posted by OmegaMan at July 18, 2009

Category: How To

Tags: ,

Sometimes there may be a need to launch a command line operation which calls either an executable or a windows command and make it hidden. Within that operation data will be returned and the main code can process it. Here is a quick example to do that where a call to IPConfig is made. By simply setting the window style to hidden one can execute something without a window flashing at the user.

ProcessStartInfo psInfo = new System.Diagnostics.ProcessStartInfo(@"ipconfig");

psInfo.WindowStyle            = System.Diagnostics.ProcessWindowStyle.Hidden;
psInfo.RedirectStandardOutput = true;
psInfo.UseShellExecute        = false;

Process GetIPInfo;
GetIPInfo = Process.Start( psInfo );

// We want to get the output from standard output
System.IO.StreamReader myOutput = GetIPInfo.StandardOutput;
GetIPInfo.WaitForExit( 3000 );

if ( GetIPInfo.HasExited )

Note This requires System.Diagnostics namespace to be included.


Posted by OmegaMan at May 27, 2009

Category: How To

Tags: , ,

This question was asked in the forums, the user had two different autocads running. Each ran from a different location and the user needed to programmatically know where it was running from. The following code snippet provides that information to the user on all processes.

// Add System.Management reference to the project
using System.Management;
using System.Management.Instrumentation;

ManagementClass mc = new ManagementClass( "Win32_Process" );

ManagementObjectCollection mcol = mc.GetInstances();

foreach ( ManagementObject strt in mcol )
   Console.WriteLine( string.Format("\t-{0}-", strt["Name"]));
   Console.WriteLine( "Path:\t" + strt["ExecutablePath"] );
   Console.WriteLine( Environment.NewLine );

Here is an example output on my box while running Windows Live Writer:


Path:   C:\Program Files\Windows Live\Writer\WindowsLiveWriter.exe


Posted by OmegaMan at October 4, 2007

Category: .Net, How To


Here is a C# example where one can execute a program using the Process Class. The example launches notepad with an argument of a filename to open and waits for completion. When the user closes notepad, its return code is printed out.


Process pr = new Process();

pr.StartInfo.FileName = "Notepad.exe";
pr.StartInfo.Arguments = "test.dat";

while (pr.HasExited == false)
    if ((DateTime.Now.Second % 5) == 0)
    { // Show a tick every five seconds.

One can extract any output that a program does to the command line by looking at the Process.StandardOutput Property if it is applicable.


Posted by OmegaMan at September 13, 2007

Category: How To


There is a quick way to have a companion browser come up with generated html from your application in C# and .Net. The following example simulates getting or generating the html, saving it to a file and then launching the file from the command line.

First off you will need to include these namespaces:
using System.Diagnostics;
using System.IO;
Then you can do this code.
string filename
    = string.Format(@"{0}\{1}",

File.WriteAllText(filename, "<h1>Hello</h1>");

  • Line 02: We assume the user does not have the final slash. A more diligent programmer will check for the slash.
  • Line 06: We simply write out the html to a file.
  • Line 08: Thanks to the OS it has assigned the extension to a launchable application and htm usually goes with the default browser. By calling the static Start on the Process Class we launch the html file.
Don’t forget to delete (or attempt to delete the file) when your program closes as a courtesy.
  • Recent Comments

    Thanks for this simple yet delightful code....
    Hey guys I'm having the same problem as Netanel KL { “Error HRESUL...
    Soluciono el problema con el primero (First step solved it for me)....
    Thank you! I was looking for this. You save my day!...
    Thank you! This was a big help. My requirement was a little differen...
    Thanks, after a small path adjustment it worked for Visual Studio 2015...
    Good call!...
    Thank you .. this helped...
    How about when you have a long string with no space? I tried it and it...
    Thank you, I ran into this same issues with Visual Studio 2015 after i...
  • Blog Stats

    • 138,615 hits