CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2006
    Posts
    97

    How to find the path of external process?

    Hi to all!

    I want to get the directory a program was launched from. These processes aren't started in my code, they are started using the usual windows explorer.
    I checked System.Diagnostics.Process and it doesn't seem it has that functionality.

    Any help appreciated!
    Using .NET 2.0

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: How to find the path of external process?

    How would you identify the process you are looking for?

    For example, if you know the process id, then you can create a Process object using System.Diagnostics.Process.GetProcessById. Or you can find all processes, using System.Diagnostics.Process.GetProcesses.

    Each process object has a MainModule property from which you can get the full pathname.
    Last edited by Zaccheus; February 18th, 2007 at 06:15 PM.
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Jun 2005
    Posts
    31

    Re: How to find the path of external process?

    Use this:

    Code:
        using System.Runtime.InteropServices;   // For P/Invoke
    
    ...
    
    public class ModFilePath
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            [System.Security.SuppressUnmanagedCodeSecurity]
     
            // DWORD GetModuleFileName(
            //   HMODULE hModule,
            //   LPTSTR lpFilename,
            //   DWORD nSize
            // );        
            private static extern uint GetModuleFileName(
                [In] IntPtr hModule,
                [Out] StringBuilder lpFilename,
                [In] [MarshalAs (UnmanagedType.U4)] int nSize);
     
            public string StartupPath
            {
                get
                {
                    StringBuilder sb = new StringBuilder(260);
     
                    if (GetModuleFileName(IntPtr.Zero, sb, sb.Capacity) == 0)
                    {
                        throw new ApplicationException("Could not retrive module file path!");
                    }
     
                    return Path.GetDirectoryName(sb.ToString());
                }
            }
        }
    Call with ModFilePath filepath = new ModFilePath(); and string localFileName = filepath.StartupPath

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: How to find the path of external process?

    You don't need to use GetModuleFileName via p/invoke, you can get full pathname from the Process class' MainModule property.
    Last edited by Zaccheus; February 19th, 2007 at 04:13 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    Jul 2006
    Posts
    97

    Re: How to find the path of external process?

    Thank you for your help!

    About process identifying, I think there's no other way to do this, but use process' name. And that's why I needed path to the exe, to check if the program was launched from directory I need.
    Using .NET 2.0

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: How to find the path of external process?

    In that case, I think you can just call System.Diagnostics.Process.GetProcesses and itereate through the returned array.
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured