Reputation: 861
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).
Resources:
Upvotes: 0
Views: 233
Reputation: 7241
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.
Upvotes: 1