Another imperfect solution (but perhaps a little closer to perfection than some others):
protected static string GetSolutionFSPath() { return System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName; } protected static string GetProjectFSPath() { return String.Format("{0}\\{1}", GetSolutionFSPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name); }
This version will return the current projects folder, even if the current project is not a Startup Project for the solution.
The first drawback is that I missed all the error checks. This can be fixed quite easily, but it should only be a problem if you save your project to the root of the drive or use the connection in your path (and this connection is a descendant of the solution folder), so this scenario is unlikely, I don’t I'm quite sure that Visual Studio can work with any of these settings anyway.
Another (more likely) problem that you may encounter is that the project name must match the folder name for the project so that it is found.
Another problem that may arise is that the project must be located inside the solution folder. This is usually not a problem, but if you used the Add Existing Project to Solution option to add a project to the solution, it may not be how your solution is organized.
Finally, if you application will modify the working directory, you must save this value before you do this, because this value is determined relative to the current working directory.
Of course, all this also means that you should not change the default values for the parameters of your projects Build → Output path or Debug → Working directory in the project properties dialog box.
krowe Oct 10 '15 at 15:16 2015-10-10 15:16
source share