Open A Visual Studio Solution From A Command Prompt Or Batch File

I got this idea from the MassTransit source code… it’s a good idea because I’m an aspiring keyboard junkie and I’m tired of mouse clicking my way to my solution files. (Note: the MT “open.bat” file is significantly more complicated than what I need or what this post talks about. I got the idea, not the implementation, from MT.)

I have 2 solutions in my source tree that I use on a very regular basis. They share a common root folder called “Source” and I run my local git repository from the sources folder which means I usually have a git bash window open in the source folder. I recently got tired of having to click around the folder structure to open the solution I want when I need it – especially when I am closing / reopening the solution whenever I pull the latest code down to my local machine (I HATE the Visual Studio “reload” dialog boxes. It’s faster and easier to close the solution and reload, IMO).

So, in an effort to be a keyboard junkie, I created these two batch files in my source folder – one for each solution. They let me open the solution I want without having to use the mouse and without having to traverse the folder structure. The contents of each batch file is this:

   1: start MySolutionFolder/MySolution.sln /D MySolutionFolder/

The “start” command will launch the default program associated with the file that is specified – in this case, the solution file I want. The “/D {folder}” option sets the working directory for the app that is launched. I’m not sure if I really need to specify the /D option, but I feel safe having done it, so it’s in there. :)

With my two batch files setup like this – one for each solution – I can run them from my git bash shell or a command prompt or powershell. Yet another way to prevent a handful of mouse clicks and folder navigation slowness.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in .NET, Command Line, Visual Studio. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.lostechies.com/members/dahlbyk/default.aspx Keith Dahlby

    It wouldn’t work as well for a project with multiple solutions, but I use the following PowerShell function to open the first .sln found recursively:

    function sln($path = ‘.’) {
    $s = ls $path *.sln -Rec | select -First 1;
    if($s) { start $s.FullName /D $s.DirectoryName }
    }

    I mostly use it without a path, but you could do “sln MySolution” pretty easily.

  • http://just3ws.com Michael D. Hall

    Slightly different approach… I have scripts in a common shared location on my user path. They just change the current working directory to the project they are named after and setup any necessary env variables. Then if I want to open the sln I just type the file name of the sln. VS is already on path so it just opens. :)

  • http://cem.me Carl Mehner

    I have started using this minutes ago to open up my different projects, many thanks for sharing your idea! One question though, why are you using a separatrix for your sub-directory separators? Isn’t the Windows standard to use a reverse solidus? (refs: http://blogs.msdn.com/larryosterman/archive/2005/06/24/432386.aspx) (I realize it works either way)

  • mendicant

    I just have Launchy index my projects folder for all .sln files. Then alt-space and just start typing the one I’m looking for. Even better than having to cd into the dir too.

  • http://www.lostechies.com/members/derick.bailey/default.aspx derick.bailey

    @carl,

    i run a bash shell all day long… unix/linux style… windows fully supports / vs \ in paths, so i use / to make it compatible across bash/cmd/powershell

    @mendicant,

    great idea! i use launchy, too… never thought about that, though. thanks!

  • mendicant

    Or, since you’re using git bash, set up an alias in your .bashrc.

    alias mysol = “cd /full/path/to/MySolutionFolder && start MySolutionFolder/MySolution.sln /D MySolutionFolder/” now you can type mysol anywhere and open it up.

  • http://www.lostechies.com/members/derick.bailey/default.aspx derick.bailey

    @mendicant,

    ya, that’s another option. the benefit of using a batch file is that you don’t have to modify the .bashrc file for every profile on every system that wants this functionality. commit the batch file to your repository and you’re good to go. plus, if you have a lot of repositories for a lot of projects, you don’t clutter up your .bashrc with a bunch of projects that you may or may not have checked out, at the moment. :)

  • Vignesh

    Is there a way to do this, but also open the VS in Administrator mode (I usually have to “Run as administrator” my Visual Studio)