在编译时的project(bin)文件夹路径? [英] Project (bin) folder path at compile time?

查看:272
本文介绍了在编译时的project(bin)文件夹路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在编译时找到项目路径?

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

我想创建一个单元测试,测试默认的web.config中的配置(项目文件夹中的一个)。主要是减少人为错误。

I want to create a unit test that tests if the configurartion in the default web.config (the one in the project folder). Mainly to reduce human error.

我不能依赖于运行时的组件位置(测试),所以我需要知道项目文件夹在哪里访问web.config。

I cannot rely on assembly locations at runtime (for the test), so I need to know where the project folder is to access web.config there.

我需要一个通用的解决方案,因为我想为多个项目使用相同的(基础)测试代码,物理位置反正对于大多数开发机器。

I need a "generic" solution since I'd like to use the same (base) test code for multiple projects, and the physical location is different anyway for most development machines.

感谢。

推荐答案

如果您想要Visual Studio项目路径,在编译时,您可以使用预构建事件(请参见项目属性对话框)运行将创建项目中使用的源文件的命令行。

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.

源文件将包含一些代码,比如变量定义。您的测试代码使用此变量。变量的值将来自VS;当它运行您的预构建事件命令时,它将项目属性替换为某些宏。你想要的宏可能是ProjectDir。

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.

这篇关于在编译时的project(bin)文件夹路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆