C# Get working directory of another process
Asked Answered
S

3

11

I want to determine the absolute path of files used by a known process by reading the command line. Currently, the process is started with relative paths in the command line that point to various files such as config files. The problem is that if the paths are not relative to the folder containing the executable, I have no way of converting the relative paths provided at the command line, well I can't be 100% sure.

For example two batch files:

BATCH 1 CD c:\test\bin test.exe ..\config\config.ini

BATCH 2 CD c:\test bin\test.exe config\config.ini

For batch file one, the command line I get is "c:\test\bin\test.exe ..\config\config.ini" and for batch file two I get "c:\test\bin\test.exe config\config.ini". So, see this I can't resolve the paths.

Anyway for starters, I got the command line from a WMI query using ManagementObjectSearcher. Now I need to get the working directory the process was started from to resolve the paths passed at the command line but how?

EDIT: I forgot one key detail. I want to get the working directory of another process. Basically, my main program gathers info from another program. I'm able to determine the process ID because I know the name of the executable. I can also determine the command line. I must now find the working directory or current directory the executable was started in so I can resolve the relative paths of command line. I hope I made the question clearer.

Socman answered 30/7, 2010 at 21:15 Comment(1)
I think the question title is not quite what you're asking. "Get process working directory" is a question of the working directory, which can presumably change during execution, but the question itself seems to be asking for the directory the executable was started from, which can be quite different (and doesn't change during process execution). @chris166's answer addresses the title of your question, but my answer addresses the question itself, IMHO. Perhaps you can clarify the confusion?Prana
S
6

I think Environment.CurrentDirectory should give you the directory the executable was started in. It is only reliable at the start of the process, because it can change later.

Or maybe try Process.GetCurrentProcess().StartInfo.WorkingDirectory. I didn't try it myself, just looked it up on MSDN

Stickup answered 30/7, 2010 at 21:25 Comment(2)
I tried all this in a console app, and Environment.CurrentDirectory came out with the directory the app started in, as you thought it would. But Process.GetCurrentProcess().StartInfo.WorkingDirectory came out as an empty string. Which surprised me -- I thought it would have been the same. A little further testing showed that if I created a shortcut to the executable, then modified the "Start In" property of the shortcut to something (like "C:\"), then that is what was then in the StartInfo.WorkingDirectory.Prana
A process's StartInfo property is almost never set. When you start a process with Powershell's Start-Process or .NET's Start(ProcessStartInfo), the property may be set, but it will only be available to the process that instantiated the ProcessStartInfo object. It is not propagated to the new process or anywhere else in the system.Zip
L
4

To get working directory (current directory) of another process in c# take a look at https://mcmap.net/q/538490/-read-other-process-current-directory-in-c

Lussi answered 27/11, 2019 at 21:43 Comment(0)
B
0

Have you tried Application.ExecutablePath?

Alternatively, there are numerous Paths that can be retrieve from Application

Beg answered 30/7, 2010 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.