Reputation: 52952
I have a project with a .txt file in a folder. The .txt file is set to Embedded Resource.
I use this code to read it:
var assembly = Assembly.GetExecutingAssembly();
var manifestResources = assembly.GetManifestResourceNames();
string s = manifestResources.Single(c => c.EndsWith("Cats.txt"));
Which works great.
However I updated Cats.txt and noticed my project didn't update.
I deleted Cats.txt and found that it is still working fine and reading the file.
I tried cleaning my solution, rebuilding, and iisreset, but it seems to insist the file is still there in the assembly despite me removing it.
Any ideas?
Upvotes: 8
Views: 5893
Reputation: 71
This happened to me in Visual Studio 2022. After copying the Resource files from another project.
Basically it was no adding it to the incorrert file. In my case I want them to be in "Texts", but it was adding them to "Resources". After choosing the right destination everything was fine again.
Upvotes: 0
Reputation: 343
Embedded files do not "automatically" refresh on build / debug.
If you want to ensure it will be done "automatically" you can in "Project Properties" insert in "Build" section, Events / Pre-built Events include:
echo "===== START - CUSTOM PRE-BUILD CLEANUP ======================="
del "$(ProjectDir)$(IntermediateOutputPath)"*.* /F /Q
del "$(ProjectDir)$(OutputPath)"*.* /F /Q
echo "===== DONE - CUSTOM PRE-BUILD CLEANUP ======================="
There are under circumstances issues with some files being removed. If that is the case you can use more granular selection.
echo "===== START - CUSTOM PRE-BUILD CLEANUP OF BIN ======================="
for %%f in ("$(ProjectDir)$(OutputPath)*.*") do if not "%%~xf"==".json" echo "Deleting %%f"
for %%f in ("$(ProjectDir)$(OutputPath)*.*") do if not "%%~xf"==".json" del "%%f" /F /Q
echo "===== DONE - CUSTOM PRE-BUILD CLEANUP OF BIN ======================="
echo "===== START - CUSTOM PRE-BUILD CLEANUP OF OBJ ======================="
for %%f in ("$(ProjectDir)$(IntermediateOutputPath)*.*") do if not "%%~xf"==".json" echo "Deleting %%f"
for %%f in ("$(ProjectDir)$(IntermediateOutputPath)*.*") do if not "%%~xf"==".json" del "%%f" /F /Q
echo "===== DONE - CUSTOM PRE-BUILD CLEANUP OF OBJ ======================="
Tested thoroughly in several projects / complex solutions. This answer applies to VS2022 as of 05/2023.
Upvotes: 2
Reputation: 430
I had the same problem, cleaning building and many other things didn't work. I had to modify the resource designer. After that, the resources were rebuil correctly
Upvotes: 2
Reputation: 8318
Rebuild solution (or startup project) updates embedded resources.
Upvotes: 2
Reputation: 131
Actually, the problem is solved by Cleaning the solution first, and then Building it again. I did not have to quit Visual Studio to update my embedded resource.
Upvotes: 9
Reputation: 52952
Okay so I fixed this by restarting visual studio. I have no idea how that affected it.
When I inspected the properties of 'assembly', the CodeBase attribute was pointing to an older copy of the DLL in a completely different folder, that was somewhere else on my hard disk, that I had open in another Visual Studio window.
I have no idea what underlying mechanism caused this to happen, I definitely had the correct version of the solution open and I was debugging it.
Closing the solution and opening it again fixed it. Chalk this one up to bizarro caching behaviour or something.
Upvotes: 1