Search code examples
visual-studiobuildvisual-studio-2017

Visual Studio: How to always replace file in output directory on build?


Visual Studio 2017 is not always copying the newest version of a file to the output directory, regardless of whether "Copy to Output Directory" is set to "Copy always" or "Copy if newer".

In a nutshell, my project includes a WebView2 control that is loading a local .html file which in turn references a local .js file. I use Directory.GetCurrentDirectory() to create the path where the .html file is located, so both files need to be copied to the output directory in order for the web view to have something to load. However, when I make changes to the .js file and then start debugging, the changes sometimes don't take. I can confirm this by comparing the actual file and the copy that is currently in the output directory, the latter of which will not have the most recent changes. Sometimes if I stop execution and then run it again, it will find the changes the second time (though sometimes it takes three or more tries). This is a nuisance, and I would like it to copy the current version of the file to the output directory whenever there are changes, but as mentioned above, it doesn't seem to matter what value I set "Copy to Output Directory" to in the file's properties.

I've tried deleting the files from the output directory, thinking that would cause VS to re-copy them, but it doesn't, and then I get an error when I run it because the files are missing. Cleaning/Rebuilding the project that contains the file also doesn't always work. So far the only thing that's worked consistently is to delete the files from the output directory, then rebuild the project that contains the changed files, and then debug the program. But the extra steps and added build time are a little bit of a pain.

Is there some other way to force Visual Studio to always copy the most recent version of a file to the output directory?


Solution

  • I did more research on what the different file properties mean, and came across this article. In a nutshell, I needed to change the Build Action from "Content" to "Embedded Resource". The latter means it will be embedded in the project, like a resource file.

    After I did that for my files, they are now always kept up-to-date.