In a .net application I need to edit an html template that I put inside a Resource file (resx).
If I edit the file from the filesystem and I build and run my application again the template remains unaffected. Even if I edit by opening it from the visual studio Resource visualizer.
The only solution I found so far is by deleting the resource from the template and add it again, also with the same name.
Is there a quicker way to mark the file as modified for the compiler to return the newer version without having to delete and recreate it?
To do this, right-click on the resource file in Solution Explorer and select "Properties".
In the Properties window, locate the "Custom Tool" property and change its value to "ResXFileCodeGenerator".
Then save the change and rebuild the project. This should cause the resource file to be regenerated and the updated version to be used in the application.
Note that changing the "Custom Tool" property will cause the resource file to be regenerated every time the project is built, so any manual changes you make to the file will be lost.
If you need to make manual changes to the resource file and have those changes persist, you can set the "Custom Tool" property back to its original value after making the changes.
Related
I have a Visual Studio 2017 solution, which is split in several C#-WinForm applications.
To have a similar user interface I put all the icons/pictures into one project (ResourceProject) and marked the resource file public.
By directly editing the .Designer-file of a Form I can now explicitly assign the Image to something from the resource file of the ResourceProject by stating
button1.Image = ResourceProject.Properties.Resources.DemoPic
However, from time to time, the designer overwrites this with
button1.Image = ((System.Drawing.Image)resources.GetObject("button1.Image")));
and creates a local resource file, which has the image embedded.
This is exactly what I wanted to avoid in the first place, since I might want to alter easily some icons later on and have those changes directly spread over all projects.
The only way to avoid it seems to be to assign the images not in the Designer file but in some other method in the actual form file.
Is there any other way around this problem?
You should not change designer.cs file, it's auto-generated and any change which you make in the file will be replaced with auto-generated code the next time which you change something in the form.
But to share image resources between multiple projects in a solution with design-time support, you can follow these steps:
Create a class library project, let's call it ResourceLibrary.
Add a Resx resource file to the root folder of the project with this name Resources.Resx.
Open the resource designer and change its Access Modifier to Public. (It will set its Custom Tool to PublicResXFileCodeGenerator)
Add a few images to the resource designer and save it.
Then in the Windows forms project do the following settings:
Add a reference to ResourceLibrary.
Right click on windows forms project and choose Add → Existing item...
Browse to the ResourceLibrary folder and choose Resources.Resx file.
Click on drop-down arrow of the Open button, and choose Add As Link.
Select Resource.Resx which has added to windows forms project and choose properties.
Set its Build Action to None
Set its Custom Tool to a text like None
Set its Custom Tool Namespace to the namespace of the resource in the other assembly: ResourceLibrary.
Rebuild the project.
Then for all the image properties you can choose the other resource file from drop-down in the Select Resource dialog. The code generation will generate correct code for the property and you have a correct build and it works as expected at design-time as well as run-time.
You can clone or download a working example here:
Repository
Download
I'm trying to create new resources files in VS2015. I created them fine, but when I try to change the "Access Modifier", the dropdown is disabled...
Any ideas?
I'm working in a ASP.NET MVC 6 (ASP.NET5). The project is a Class Library, but I have tested in Web Project with same results.
Thanks!!
Edit: Added Properties window
Can you check the property of your file and verify if Custom Tool is ResXFileCodeGenerator ? If it's GlobalResourceProxyGenerator, the dropdown will be disabled.
I had the same problem but I found this easy solution Visual Studio's Access Modifier drop down option is disabled for resource file
To summarize:
Right click on your file resource, choose Properties (Alt+Enter)
Change Build Action to Embedded Resource
Change Custom Tool to PublicResXFileCodeGenerator
A) In Asp.net Core projects
This problem is a known bug in Asp.net Core projects and access modifier is on public by default and you can not change it. It will be solved by asp.net core team next updates but if you need internal access modifier you can use my temporary solution:
Add your all items by the resource designer in your Resource.resx and save it
In the solution explorer expand the Resource.resx tree and open Resource.Designer.cs
Replace all public strings in it with internal and save it
Note: every time you save the Resource.resx file you should do the step 3 again.
Finally you should have a Resource.Designer.cs file with access modifiers like this:
Also check the namespace in Resource.Designer.cs file. it should be a appropriate namespace. Sync with your project namespace.
B) In Normal Asp.net projects
If you have not CustomTool property in the Properties panel for your resource (.resx) file to change it to
PublicResXFileCodeGenerator and solve the problem
Then you should change some settings in your project (.csproj) file manually. It's so easy, just follow my instructions:
Right click on your project in solution explorer and select Unload Project
Right click again on it and select Edit .....csproj
In the opened .csproj file, find the .resx string, you will see a block of settings there. That is something like bellow codes.
Change it to something like the following code (include
PublicResXFileCodeGenerator):
.
<EmbeddedResource Include="MyResourceFile.resx">
<SubType>Designer</SubType>
<Generator>PublicResXFileCodeGenerator</Generator> <!--important line-->
<LastGenOutput>MyResourceFile.Designer.cs</LastGenOutput>
</EmbeddedResource>
Save the edited .csproj file
Right click again on your project in the solution explorer and select Reload project
Now open your .resx file and enjoy ;)
Note: use your resource file name instead of MyResourceFile
I've been having a similar problem.
This appears to be a known issue: https://github.com/aspnet/Tooling/issues/339
I have two assemblies in my application one is where all the code is, the other holds all the images and extra resource files. It was working just fine until I added new images to the resource dll. Now it keeps coming up with MissingManifestResourceException and saying it can't find certain images. All my images are being compiled as Resources.
Fix this problem as right-click the Resources.resx file in the Solution Explorer and click Run Custom Tool. This re-generates the auto-generated Resources.Designer.cs file.
If the .resx file was added to the project manually, the Custom Tool property of the file must be set to "ResXFileCodeGenerator".
Rebuilt that project and then run it
I have a file in a folder of my C# VS2010 project marked Content and Always Copy.
When I change this file and save it, externally to VS, and then run the project, the modified file is not copied to the output. If I rebuild, then it is. My guess is that VS does not use the modified date time stamp of the file to determine whether it should be copied or not.
Am I missing something? It's driving me nuts as I'm losing 5 minutes per run.
Thank you!
Hard to explain so start by getting better diagnostics. Tools + Options, Projects and Solutions, Build and Run settings. Change the "MSBuild project build output verbosity" setting to Normal. In that same property page, ensure that the "On Run, when projects are out of date" setting is set to Always build. Look in the Output window when you press F5 or Build + Build, the _CopyOutOfDateSourceItemsToOutputDirectory task is the one that copies the file.
One possible trap when you use Project + Add Existing Item: the IDE makes a copy of the file that you selected. You might be changing the original file instead of the copy in the project directory. You'd fix that by clicking the arrow on the Add button in the dialog and selecting "Add As Link".
The behavior you are expecting can be achieved by changing the Build Action from "Content" to "Embedded Resource".
Came across this when I needed to use COMFileReference but also needed to copy the DLL to the output folder.
In the end, ending up adding the DLL twice to the project, once as Content for the copy to output folder and once as COMFileReference. Easiest way I found was to modify the .csproj xml directly.
I have an xslt file which I initially added to the "Resources" section of my project through the project properties. Inside of my program I call Resources.MyXsltFile to grab the string and I load it using XslCompiledTransform.Load. My program ran fine initially but after I updated the xslt file I noticed that my Resources.MyXsltFile was still using the original xslt contents and not my updated version. What's the deal?
It turns out that the compiled "Resources.Designer.cs" file that goes along with the 'Resources.resx' for the project does NOT get regenerated when you update one of the resource files. Therefore you have to regenerate the "Resources.Designer.cs" file manually. Here is an article from Marek Grzenkowicz which shows how to regenerate the file by right-clicking on "Resources.resx" and clicking on "Run custom tool". After that I ran my program and it finally used the updated version of my resource file.
Also, if you do not want to use Custom Tool solution, make sure that your resource is using not at "No Code Generate" at the Access Modifier.