How to get the path of the current csproject?
Let’s say I have a csproject name MyProj.csproj, inside there is a class that wants to know what is the file path of this csproj, any idea on how to do this? thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you really must do this, I would consider meta-programming. Create another project in your solution that builds a command line exe which takes one argument. The command line exe project should output a cs file that contains a class with a static property returning the value passed at the command line.
Then, as a pre-build step of your other project, call this command line exe (you’ll need your build order such that the command line exe is built first) with the ProjectDir variable so that the command line argument is your project dir.
You’ll also need your csproj to include the file that will be output by the command line so that it gets incorporated into your compiled assembly. Your main assembly code can call the static property of the generated class and so access the path.
Of course, just because you can do this, doesn’t mean you should; I’m just assuming you’ve already determined that you must do this for your project.