.net 在 Visual Studio 调试会话期间查找当前目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1367732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:15:49  来源:igfitidea点击:

Finding current directory during Visual Studio debugging session?

.netdebuggingprocess-explorer

提问by Peter Mortensen

How can I find the current directory for a .NET application running under the Visual Studio debugger?

如何找到在 Visual Studio 调试器下运行的 .NET 应用程序的当前目录?

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.

更新 1。要明确:我不想更改代码或获取程序本身的信息 - 我只想获取有关当前正在调试的应用程序的信息。

While debugging a .NET Windows Formsapplication (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:

在调试 .NET Windows 窗体应用程序(混合 VB.NET 和 C#)时,我不确定从哪个位置读取 XML 文件。我希望当前目录是应用程序的目录。但是,使用 Process Explorer,流程的属性会导致:

D:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\

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):

将光标悬停在 Process Explorer 主视图中的进程上会显示不同的结果(请参见下面的屏幕截图):

D:\dproj\DTASCall\DTASuperCharge\bin\

D:\dproj\DTASCall\DTASuperCharge\bin\

What is correct?

什么是正确的?

Starting the application standalone displays the expected current directory,

独立启动应用程序显示预期的当前目录,

D:\dproj\DTASCall\DTASuperCharge\bin\

D:\dproj\DTASCall\DTASuperCharge\bin\

in the Process Explorer process properties window.

在 Process Explorer 进程属性窗口中。



Annotated screen-shot of Process Explorer:

Process Explorer 的带注释的屏幕截图:

Alt text http://www.pil.sdu.dk/1/until2039-12-31/PEdiscrepancy_2009-09-02.png

替代文本 http://www.pil.sdu.dk/1/until2039-12-31/PEdiscrepancy_2009-09-02.png

回答by Philip Rieck

In Visual Studio, under the project's settings in the debug tab, you can set the "Working Directory" if you want.

在 Visual Studio 中,在调试选项卡的项目设置下,您可以根据需要设置“工作目录”。

To determine the current working directory in code or in the immediate window in a breakpoint, try

要确定代码中的当前工作目录或断点的直接窗口中,请尝试

System.IO.Directory.GetCurrentDirectory()

回答by Richard

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).

默认情况下,除非您更改了项目的 Debug 属性,否则当前目录将作为项目的 bin\Debug 目录(运行 .exe 的位置)开始。

回答by steve

The best way is to run the application in WinDbg(the Windows debugger), then attach to the process and run the !handlecommand. Each open file will have an associated handle. By dumping all the handles for the corresponding process, you will see the corresponding file path.

最好的方法是在WinDbg(Windows 调试器)中运行应用程序,然后附加到进程并运行!handle命令。每个打开的文件都有一个关联的句柄。通过转储相应进程的所有句柄,您将看到相应的文件路径。

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 !htracecommand.

将 process-id 替换为您的进程 ID 的值。您还可以使用进程地址代替进程 ID。如果这没有显示文件对象,则文件句柄已经关闭。在这种情况下,您需要跟踪句柄。这可以通过!htrace命令来完成。