How to Get a Visual Studio Solution Path in C#

(0 votes, average 0 out of 5)

Getting a path to the current solution in C# can be difficult because the idea of a Visual Studio solution is abstracted away from the actual code that is running.  But not all is lost -- Visual Studio builds it's binaries into the same location unless you specify them otherwise, and we can use this to our advantage.  For instance, if you have some project ExampleProject,  obtaining the current path while running the code would result in this:

 

[solution path]\ExampleProject\bin\debug\

 

So to get the solution path, we simple need the following code:

1
2
3
4
5
6
using System.Reflection
 
string cwd = System.Reflection.Assembly.GetExecutingAssembly().Location
string projectName = "ExampleProject"  //YOUR PROJECT NAME HERE
 
string solutionPath = cwd.Replace(projectName + "\\bin\\Debug", "");

This code should return the solution path that the binaries are running in.  Getting the solution path is typically not recommended for a variety of reasons, but can be useful for debugging and logging, or if you have no intention to publish your app.

Partner Links:
Last Updated on Tuesday, 14 February 2012 12:40  
Related Articles

» For Loops in PHP

To create a for loop in php, follow these simple steps:Set a counter variable to some initial value.Check to see if the conditional statement is true.Execute the code within the loop.Increment a counter at the end of each iteration through the loop.The following is pseudo php code for a for loop:123for(init counter; conditional statement; increment counter){ inner code;}As you can see, all of the above steps are taken care of within the for loop, making the syntax short and easy to...

» Introduction to LaTeX

LaTeX is a self-described document preparation system that is often used for research papers or other technical publications.  It is, in essence, an extremely useful computer language that interprets code and produces a well-formatted document.  It can greatly increase productivity due to the ease of formatting, and other nicities like automatic figure and reference numbering and easy mathematical representation.  Unfortunately, it also have a very steep learning curve. ...

» How to Shutdown or Restart Windows From the Command Line

To shutdown or restart windows from the command line or using a shortcut, you must use the Shutdown.exe program. To use this program, open up a command prompt window. This can be done by navigating to Start -> Run and typing in 'cmd'.Once the command prompt is open, use the following command:shutdown [-i | -l | -s | -r | -a] [-f] [-m computername] [-t xx] [-c "comment"] [-d up:xx:yy]Arguments:  No args Display this message (same as -?)-i Display GUI interface, must be the...