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.
-
Unless you are talking about creating a plugin for Visual Studio, the CsProj file no longer exists (as far as your code is concerned) once your program compiles and is running.
The CsProj is actually used to instruct the compiler how to compile your C# code into an assembly. After that point the CsProj is left on the developer's workstation as it is not needed by the "end user" of your application.
Marc Gravell : Just additional for the OP: there doesn't even need to *be* a csproj - you can build directly with "csc" etc.From Jason Whitehorn -
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.
From Jeff Yates
0 comments:
Post a Comment