Visual Studio Protocol Handler - open file

Is there a protocol handler in Visual Studio that includes a command to open a specific file?

They have one for the Git clone, as described in the GitHub Extension Announcement for Visual Studio :

The Open button in Visual Studio [on GitHub] calls up a new protocol handler called "git-client: //". We developed this new common protocol with GitHub so that the website can transfer standard Git operations to any IDE.

Essentially, what I want to do is described in this GitHub Issues :

enter image description here

When I click on the button, Visual Studio should open a specific file (preferably with the correct solution loaded).

If this is not possible directly with protocol handlers, can this be done as a Visual Studio extension with a web view that will achieve the same goal (i.e., allow files to be opened from a web page)?

+8
source share
1 answer

I managed to get close enough to what I wanted by creating a Visual Studio plugin with a standalone Owin server providing basic WebApi.

This allowed me to open files from a browser with the link: http: // localhost: 9000 / VisualStudioApi / OpenFile? Path =. \ Url \ Escaped \ Path \ Relative \ To \ Solution \ File

Opening Files in Visual Studio Using a Web Browser

Any web server hosting this button will have to hardlink the links to http://localhost:9000 , which causes a problem with launching multiple instances of Visual Studio, so there should be some logic on how to map the .sln file with famous port. But the missing official Visual Studio solution, this does the job for the most part.


In case this helps someone in the future, here are the code snippets:

VS package

 [ComVisible(true)] [Guid("B77F7C65-0F9F-422A-A897-C06FDAEC9604")] [ProvideObject(typeof(InitializerPackage))] [ProvideAutoLoad(UIContextGuids80.SolutionExists)] public class InitializerPackage : Package { protected override void Initialize() { base.Initialize(); //Get copy of current DTE var dte = (DTE)GetService(typeof(DTE)); var dte2 = dte as DTE2; dte2.Events.SolutionEvents.Opened += () => OwinVisualStudioApiListenerManager.StartServer(dte2); dte2.Events.SolutionEvents.AfterClosing += () => OwinVisualStudioApiListenerManager.StopServer(); } } 

Owin Initializer

 public static class OwinVisualStudioApiListenerManager { private static IDisposable _runningServer; public static DTE2 VisualStudioApi { get; set; } public static void StartServer(DTE2 visualStudioApi) { if (null != _runningServer) _runningServer.Dispose(); VisualStudioApi = visualStudioApi; //nothing fancy about OwinStartup //see github file http://tinyurl.com/zt2bm8b _runningServer = WebApp.Start<OwinStartup>("http://localhost:9000"); } public static void StopServer() { if (null != _runningServer) _runningServer.Dispose(); VisualStudioApi = null; } } 

Webapi

 public class VisualStudioApiController : ApiController { // GET /VisualStudioApi/OpenFile/?path= [HttpGet] public string OpenFile(string path) { var fullPath = Path.Combine( Path.GetDirectoryName( OwinVisualStudioApiListenerManager.VisualStudioApi.Solution.FullName), HttpUtility.UrlDecode(path)); //http://stackoverflow.com/q/5039226/1224069 OwinVisualStudioApiListenerManager.VisualStudioApi .ExecuteCommand( "File.OpenFile", fullPath); return "success"; } } 
+4
source

Source: https://habr.com/ru/post/1239849/


All Articles