locked
System.Diagnostics.Process.Start() returns false RRS feed

  • Question

  • I'm writing a simple C# console app (in Windows Vista) that gets a file and has to open it in the default editor for that file type.  The problem I'm having is that when launching applications that are tied to the OS (Media Player, Photo Gallery, etc)  the Process.Start() call returns false and doesn't bind any of the process information to the Process object.  At a minimum I need to know when this process exits.  Is there a way to do this using the Process object or is there some other work-around?

    I'm using code similar to this:

    private void execProc(String path, String fileName) {

       System.Diagnostics.Process proc = new System.Diagnostics.Process();

       proc.StartInfo.FileName = (path + fileName);

       proc.Start();

       Console.WriteLine("Process: " + proc.ToString() +
                         "\nName: " + proc.ProcessName +
                         "\nPID: " + proc.Id);
    }

    If I make this call:  execProc(".\\", "foo.pdf")
    This is the output:
    Process: System.Diagnostic.Process (AcroRd32)
    Name: AcroRd32
    PID: 38788

    If I make this call: execPro(".\\", "foo.jpg")
    This is the output:
    System.InvalidOperationException: No process is associated with this object.
    at ... <stack trace>

    Note that before the exception occurs, foo.jpg is opened in Windows Photo Gallery, so the proc.Start() works, it just doesn't bind the process to the proc object.
    Monday, January 21, 2008 8:23 PM

Answers

  • It returns false because no new process resource is started (for example, if an existing process is reused), since that Windows Photo Gallery is not a process, instead,  it is just a child window spawn by the windows explorer process.
    Wednesday, January 23, 2008 10:40 AM
  • When the Jpg file is opened, the title of the window will be "FILENAME.jpg - Windows Photo Gallery", you can use FindWindow Function () to retrieve a handle to the top-level window whose class name and window name match the specified strings.

    Check out this reference for an example on this: FindWindow Alternative in C# .NET
    Thursday, January 24, 2008 7:56 AM

All replies

  • It returns false because no new process resource is started (for example, if an existing process is reused), since that Windows Photo Gallery is not a process, instead,  it is just a child window spawn by the windows explorer process.
    Wednesday, January 23, 2008 10:40 AM
  • That makes sense.  Is there any way to get a handle on the new window so my program can tell when it has closed?
    Wednesday, January 23, 2008 2:20 PM
  • When the Jpg file is opened, the title of the window will be "FILENAME.jpg - Windows Photo Gallery", you can use FindWindow Function () to retrieve a handle to the top-level window whose class name and window name match the specified strings.

    Check out this reference for an example on this: FindWindow Alternative in C# .NET
    Thursday, January 24, 2008 7:56 AM