0

Visual Studio can navigate to source files of .NET assemblies from NuGet, see Source Link I would like to enhance existing Visual Studio extension called Open on GitHub. My goal is to add the ability to navigate to the Source Link file in Web directly from Visual Studio. However, I'm struggling to determine how to retrieve the dll or pdb file location by the active document (the dll location is available in the tooltip - see the screenshot).

Source Link specific information

Resources:

Upd: I have not found the good solution yet, so I copied that information from the document tab tooltip.

1
  • Try getting info from the pdb file. :) Commented Apr 11, 2024 at 8:24

1 Answer 1

1

Is there a way to programmatically retrieve the URL from which Visual Studio downloaded a file?

There should have no open API to achieve this, but meta file pdb should be able to achieve some information:

using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;

class Program
{
    static void Main(string[] args)
    {
        string pdbPath = @"C:\Users\Administrator\Downloads\Newtonsoft.Json.pdb"; // Specify the path to your PDB file
        string sourceLinkJson = GetSourceLinkJson(pdbPath);

        if (sourceLinkJson != null)
        {
            Console.WriteLine("Source Link JSON:");
            Console.WriteLine(sourceLinkJson);
        }
        else
        {
            Console.WriteLine("Source Link information not found.");
        }
    }

    static string GetSourceLinkJson(string pdbPath)
    {
        // The well-known Guid for Source Link
        Guid sourceLinkGuid = new Guid("CC110556-A091-4D38-9FEC-25AB9A351A6A");

        using (FileStream stream = File.OpenRead(pdbPath))
        using (MetadataReaderProvider metadataReaderProvider = MetadataReaderProvider.FromPortablePdbStream(stream))
        {
            MetadataReader metadataReader = metadataReaderProvider.GetMetadataReader();
            foreach (var handle in metadataReader.CustomDebugInformation)
            {
                var cdi = metadataReader.GetCustomDebugInformation(handle);
                var cdiKindGuid = metadataReader.GetGuid(cdi.Kind);

                if (cdiKindGuid == sourceLinkGuid)
                {
                    // If the kind matches the Source Link Guid, get the value
                    var value = metadataReader.GetBlobBytes(cdi.Value);
                    return System.Text.Encoding.UTF8.GetString(value);
                }
            }
        }
        return null;
    }
}

Is it possible to determine the type of source control provider (GitHub, GitLab, Bitbucket, etc.) based on the downloaded file's content or metadata?

You can judge this based on the information contained in the URL.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answering, how do we know where the pdb is based on the active document information? (I added question #3) There is a tooltip and some information in the Output Tab - see screnshot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.