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

How to find corresponding .pdb inside .NET assembly?

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

我如何访问它?我曾尝试使用 ILSpy 查看我的程序集,但找不到。

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

dumpbin /HEADERS YourAssembly.exe   

将在调试目录部分显示 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

...

我找到了以下 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('[=10=]');
    return s.Substring(lastTerminatorIndex + 1, pdbIndex - lastTerminatorIndex + 3);
}

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

一段时间过去了,现在我们有了时髦的新 .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;
        }
     }
  }