8

Apparently, when a .NET assembly is created the location of the corresponding .pdb file path is included inside. Link for reference: https://msdn.microsoft.com/en-us/library/ms241613.aspx

How do I access this? I have tried using ILSpy to look inside my assembly but could not find.

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
Andrew Pham
  • 185
  • 1
  • 11

3 Answers3

3

You can use the dumpbin tool from a Developer Command Prompt, e.g. a cmd line like this

dumpbin /HEADERS YourAssembly.exe   

would show the path to the PDB file in the Debug Directories section similar to this

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

...
0xced
  • 25,219
  • 10
  • 103
  • 255
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
0

I have come to the following hacky solution.
It works for me, but I cannot guarantee its correctness :)

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);
}
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

Some time has passed, and now we have funky new .net core tools.

You can now do this easily:

  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;
        }
     }
  }
Kinetic
  • 700
  • 8
  • 15