问题
How can I find the current directory for a .NET application running under the Visual Studio debugger?
Update 1. To be clear: I don't want to change the code or get information in the program itself - I just want to get information about the application currently being debugged.
While debugging a .NET Windows Forms application (mixed VB.NET and C#) I was not sure from which location a XML file was being read from. I expected the current directory to be the application's directory. However, using Process Explorer, properties for the process result in:
D:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\
(right click on process/Properties/tab Image/Current Directory).
Hovering the cursor over the process in the main view of Process Explorer revealed a different result (see below for a screenshot):
D:\dproj\DTASCall\DTASuperCharge\bin\
What is correct?
Starting the application standalone displays the expected current directory,
D:\dproj\DTASCall\DTASuperCharge\bin\
in the Process Explorer process properties window.
Annotated screen-shot of Process Explorer:
Alt text http://www.pil.sdu.dk/1/until2039-12-31/PEdiscrepancy_2009-09-02.png
回答1:
In Visual Studio, under the project's settings in the debug tab, you can set the "Working Directory" if you want.
To determine the current working directory in code or in the immediate window in a breakpoint, try
System.IO.Directory.GetCurrentDirectory()
回答2:
Within your code, call the function
System.IO.Directory.GetCurrentDirectory()
By default, unless you've changed the Debug properties of your project, the current directory will start as the bin\Debug directory of your project (where the .exe runs from).
回答3:
The best way is to run the application in WinDbg (the Windows debugger), then attach to the process and run the !handle
command. Each open file will have an associated handle. By dumping all the handles for the corresponding process, you will see the corresponding file path.
Here is an example:
!handle 0 f process-id
Replace the process-id with the value of your process id. In place of the process id you can also use the process address. If this does not show the file object, then file handle has already been closed. In this case you need to trace the handles. This can be done with the !htrace
command.
来源:https://stackoverflow.com/questions/1367732/finding-current-directory-during-visual-studio-debugging-session