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

» 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...

» How to Print in Python

Printing in Python is as simple as typing print followed by the string you wish to print.  You do not need to import any libraries.12>>> print "Hello World"Hello World You can also print the value of an integer or list simply by print 123456>>> x = [1, 2, 3]>>> print x[1, 2, 3]>>> y = 2>>> print y2If, however, you want to print a string AND some integer (or list), you need to cast to string.123>>> x = 42>>> print "The meaning...

» Comments in Python

Line CommentsLine comments in Python are very simple.  The # character denotes that everything afterward on that line is a comment.  12#here is a whole-line comment for some codex = 3 #this comments code on the current line Block CommentsUnfortunately, there is no specific syntax for block comments in python.  To comment a block of code, you must simply place a # before every line in the block of code you wish to comment.  Many Python IDE's, however, provide a simple way...