Copying Visual Studio project file(s) to o

2019-01-22 11:27发布

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).

7条回答
Rolldiameter
2楼-- · 2019-01-22 12:01

For copying a files to the output directory in Visual Studio 2003 you could use Post-Build event:

  1. Right click on the project->Properties
  2. Common Properties->Build Events
  3. Set Post-Build Event Command Line to:

    xcopy /y $(ProjectDir)my_file.ini  $(ProjectDir)$(OutDir)
    
  4. OK and build!

查看更多
别忘想泡老子
3楼-- · 2019-01-22 12:02

For VS 2017 the command Dmitry Pavlov posted would be the following:

xcopy /y "$(ProjectDir)my_file.ini"  "$(OutDir)"

Quotes are important in case there are spaces in the path to the project directory.

查看更多
女痞
4楼-- · 2019-01-22 12:08

You need the extra $(OutDir). Otherwise, in the rebuild/clean step it will throw away your source.

CommandLine : copy "$(SolutionDir)last-script.js" "$(TargetDir)Debug"

Outputs  : $(TargetDir)Debug\last-script.js
查看更多
在下西门庆
5楼-- · 2019-01-22 12:11

In case this helps anyone, I needed to copy the output dll of the project i was building into another project.

xcopy /y "$(ProjectDir)$(OutDir)$(TargetName)$(TargetExt)" 
"C:\Application\MyApplicationName\bin\x86\Debug"

/y = overwrite file if already exists
$(ProjectDir) = location on your machine where the project lives
$(OutDir) = is where your current build setup outputs the build
$(TargetName) = What the project being built is set to be called. Ex: XXX of XXX.dll
$(TargetExt) = the extension of the build Ex: .dll of XXX.dll

"C:/..../x86/Debug" is the location to copy to.
查看更多
狗以群分
6楼-- · 2019-01-22 12:13

Improving Synetech answer :

In VS 2013 C++ project Command Line : copy %(Identity) $(OutDir) Description : Copying foobar... Outputs : %(Identity) It works , But it leads to circular dependency , i.e. it will be executed each time you demand increamental build, no meter it has been already copied.

To solve this , you can add that item at target folder, change path to $(OutDir), and use that in first added item as Output. Drawback - two items with similar name are in solution.

Also usefull xcopy with /d /y parameters in postbuild - copy only if target file date is older.

查看更多
闹够了就滚
7楼-- · 2019-01-22 12:21

Please try select the file in Solution Explorer. Then you should be able to see its properties in Properties window (press F4 if it is not visible). You will find there two properties:

  • "Build Action" and
  • "Copy to Output Directory"

Set "Build Action" to "Content", and then - select an appropriate value for "Copy to Output Directory" setting.

File properties window with "Build Action" and "Copy to Output Directory" settings

File properties window with "Build Action" and "Copy to Output Directory" settings

If the way above doesn't work for you, please read this post "Copy to output directory issue with .inf file". And have a look at this one then "Visual Studio: default build action for non-default file-types"

Hope that helps

查看更多
登录 后发表回答