如何在.NET程序集中找到相应的.pdb?

时间:2016-08-08 05:09:39

标签: c# asp.net .net debugging pdb-files

显然,创建.NET程序集时,相应的.pdb文件路径的位置包含在其中。链接供参考: https://msdn.microsoft.com/en-us/library/ms241613.aspx

如何访问此内容?我已经尝试使用ILSpy查看我的程序集但无法找到。

3 个答案:

答案 0 :(得分:2)

您可以使用开发人员命令提示符中的dumpbin工具,例如像这样的cmd行

dumpbin /HEADERS YourAssembly.exe   

将在Debug Directories部分中显示PDB文件的路径,类似于此

Microsoft (R) COFF/PE Dumper Version 14.00.24213.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file YourAssembly.exe

...


  Debug Directories

        Time Type        Size      RVA  Pointer
    -------- ------- -------- -------- --------
    570B267F cv           11C 0000264C      84C    Format: RSDS, {241A1713-D2EF-4838-8896-BC1C9D118E10}, 1,  
    C:\temp\VS\obj\Debug\YourAssembly.pdb

...

答案 1 :(得分:1)

我遇到了以下hacky解决方案 它适用于我,但我无法保证其正确性:))

public string GetPdbFile(string assemblyPath) 
{
    string s = File.ReadAllText(assemblyPath);

    int pdbIndex = s.IndexOf(".pdb", StringComparison.InvariantCultureIgnoreCase);
    if (pdbIndex == -1)
        throw new Exception("PDB information was not found.");

    int lastTerminatorIndex = s.Substring(0, pdbIndex).LastIndexOf('\0');
    return s.Substring(lastTerminatorIndex + 1, pdbIndex - lastTerminatorIndex + 3);
}

public string GetPdbFile(Assembly assembly) 
{
    return GetPdbFile(assembly.Location);
}

答案 2 :(得分:0)

一些时间过去了,现在我们有了时髦的新.net核心工具。

您现在可以轻松地做到这一点:

  private static void ShowPDBPath(string assemblyFileName)
  {
     if (!File.Exists(assemblyFileName))
     {
        Console.WriteLine( "Cannot locate "+assemblyFileName);
     }
     Stream peStream = File.OpenRead(assemblyFileName);
     PEReader reader = new PEReader(peStream);         

     foreach (DebugDirectoryEntry entry in reader.ReadDebugDirectory())
     {
        if (entry.Type == DebugDirectoryEntryType.CodeView)
        {
           CodeViewDebugDirectoryData codeViewData = reader.ReadCodeViewDebugDirectoryData(entry);
           Console.WriteLine( codeViewData.Path);
           break;
        }
     }
  }