且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在构建期间将 Visual Studio 项目文件复制到输出目录

更新时间:2022-10-20 19:04:04

当我在文件的属性页中搜索构建操作字段时,我有一个想法:将自定义构建步骤设置为 copy 文件(手动).结果证明这比我想象的要容易.我认为这需要使用 cmd 或其他外部可执行文件(xcopyrobocopy 等),但这不是必需的.>

我将自定义构建步骤设置如下:

命令行:复制 $(InputFileName) $(OutDir)描述:复制 foobar...输出:$(InputFileName)

设置输出字段(正确)是至关重要的,以防止 VS 总是认为项目已过时并需要重建(我不确定它是否需要以 $(OutDir)).

它反映在输出窗口中,如下所示:

正在复制 foobar...已复制 1 个文件.编译资源...正在链接...

When I build a Visual Studio project, the executable is written to the output directory specified in the projects Property Page.

I have a project that has some extra files (e.g., .ini file) that are used by the program.

How can I configure the project to copy the file to the output directory so that when the program runs, it has a copy of the other file in its CWD?

I checked the Property Page of the file and there was nothing useful other than an option to exclude it from the build (which is disabled), and the custom-build-tool command is empty (plus it is a plain-text file that does not need any processing).

While I was searching the file’s Property Page for a build-action field, I had a thought: set the custom build step to copy the file (manually). This turned out to be easier than I thought. I had figured it would require using cmd or other external executable (xcopy, robocopy, etc.), but that is not necessary.

I set the Custom Build Step as follows:

Command Line : copy $(InputFileName) $(OutDir)
Description  : Copying foobar...
Outputs      : $(InputFileName)

Setting the outputs field (correctly) was critical in order to prevent VS from always thinking the project is out of date and requiring to be rebuilt (I’m not certain if it needs to be prefixed with $(OutDir)).

It is reflected in the Output window as such:

Copying foobar...
        1 file(s) copied.
Compiling resources...
Linking...