VS PostBuild Event - Copy file if it exists

后端 未结 3 1025
情深已故
情深已故 2021-02-18 13:57

Is there a simple way to copy a file in a post-build event for a Visual Studio project, but only if the file exists? (i.e. don\'t fail the build if the file doesn\'t exist)

3条回答
  •  旧巷少年郎
    2021-02-18 14:17

    If you prefer to use "Copy to Output Directory" in the Properties panel (provided by Visual Studio when you right-click on a project-file and select "Properties"), you can do so with a little bit of text-editing. This will work for all versions of Visual Studio that use MSBuild (i.e. Visual Studio 2010 and onward).

    First, choose an appropriate value for "Copy to Output Directory", such as "Copy always" or "Copy if newer". Save your changes to the project (e.g. "Save all" from the "File" menu).

    Then, edit the project file in a text editor, and inside the opening tag of the newly-added element, add an attribute like Condition="Exists('$(MSBuildProjectDirectory)\FILENAME')", ,where FILENAME is the path of the file being copied (i.e. as referred to in the parent element).

    The GUI won't edit this setting properly in the future (i.e. it'll display "Do not copy" as the value if the file doesn't exist), but at least it'll work.

提交回复
热议问题