Project (bin) folder path at compile time?

后端 未结 3 1261
暗喜
暗喜 2021-01-01 06:01

Is there a way to find out the project path at compile time?

I want to create a unit test that tests if the configurartion in the default web.config (the one in the

相关标签:
3条回答
  • 2021-01-01 06:08

    To improve the solution slightly, instead of using the Post Build Event Command Line, you can run the command as an MSbuild Exec Task in the BeforeBuild Target of the project.

    0 讨论(0)
  • 2021-01-01 06:09

    Based on rkb's answer,

    As it sounds like you've got a C# project, use this post build step.

    echo namespace ProjectPath { static public class ProjectPath { public static readonly string Path = @"$(ProjectDir)";} } > $(ProjectDir)path.cs
    

    Then include path.cs as an existing item to your test project. Then you can access it via:

    string path = ProjectPath.ProjectPath.Path;
    
    0 讨论(0)
  • 2021-01-01 06:11

    If you want the Visual Studio project path, at compile time, you could use a Pre-Build Event (see the Project Properties dialog) to run a command line that will create a source file used in your project.

    The source file will contain some code, say a variable definition. Your testing code uses this variable. The value of the variable will come from VS; when it runs your Pre-Build Event command, it substitutes project properties for certain macros. The macro you want is probably ProjectDir.

    So in the end, you have something like this for your Pre-Build Event's command:

    echo 'const char * PROJECT_PATH = "$(ProjectDir)";' > source.cpp
    

    Not sure what language you're using, so adjust accordingly.

    0 讨论(0)
提交回复
热议问题