Programmatically launching Visual Studio with parameters

In the group where I work, we don’t open Visual Studio (VS) solutions by double-clicking on them in Windows explorer, rather we have a special command-line launcher that takes care of that. The reasons why are unimportant, what I want to focus on in this post is the programmatic launching of Visual Studio when you want to pass it parameters beyond the file name.

Suppose you have the path of an SLN file in your hands, and you want to launch it in VS programatically (let’s say using C#). Assuming VS is registered as the default application for SLN files (a reasonable assumption), the solution is pretty simple:

Process.Start("foo.sln)"

. But what happens if you want to run it with a switch, say /Build debug? The previous approach won’t work. Instead, there are two things you need to do:

1. Find out the full path of the application registered to open SLN files – remember, you want this tool to work on any computer, regardless of where VS is installed (and which version is installed). Note that this application actually won’t be devenv.exe (VS’s executable), rather it will be VSLauncher.exe which analyzes the SLN file and decides which VS version to open. Finding its path is done by examining the registry, and I posted a function on StackOverflow that does exactly that.

2. Start VSLauncher.exe with the desired command-line:

Process.Start(vsLauncherPath, 
  string.Format(""{0}" /Build "debug"", solutionPath));

(note the escaped quotes (“) – they are necessary due to the way VSLauncher passes parameters to devenv)

Another usage example is elevating VS’s process priority on startup. As professional .NET developer, we spend most of our working days in VS, and we want it to be as fast as possible. Of course, setting its process priority as “High” manually every time is easy enough, but that’s easy to forget. Enter devenv.exe’s /Command switch, which allows you to run any VS command on startup. A command in VS can be many thing, but we’ll use it to run a macro (VS 2012 dropped macro support so this method won’t work for it).

  1. Open Visual Studio (tested on 2010 but should work in earlier versions).
  2. Hit Alt+F11 to get to the macros window.
  3. In the left pane, expand “Samples” -> “Utilities” and add a new function named RaisePriority with the following implementation:
    For Each proc As System.Diagnostics.Process In 
      System.Diagnostics.Process.GetProcessesByName("devenv")
     proc.PriorityClass = ProcessPriorityClass.High
    Next
    
  4. Save

Now you can use the following code to automatically launch VS with high priority:

Process.Start(vsLauncherPath, 
  string.Format(""{0}" /Command "Macros.Samples.Utilities.RaisePriority"", solutionFile))

(again,note the escaped quotes – they are necessary!)

Leave a Reply

Your email address will not be published. Required fields are marked *