how to put computed value in RESX at build time C# - c#

I have a situation where in I have a string that should contain today's date in it..
the issue is that I need to put that string including today's date in the .resx file.
is there any way to do that?
e.g.
lets say about copyright string
it should be copyright-2012-abc is today's date is of year 2012
&it should be copyright-2013-abc is today's date is of year 2013
EDIT:
I m going to use the resource file for this copyright thing only.
it will be static for all other things just that year value will be changing with every passing year just like in the example.

There is no built-in way to add "current environment" values to RESX (or any other C# related files).
But you can relatively easy create pre-build rule that will call your custom script/tool that will modify your RESX file to your liking.
One option it to create tool that will genreate a file that later included in the RESX file. This way you avoid modification of RESX file itself (which could be problematic if RESX is under source control). Depending on your need simple date /t > ..\..\out.txt with out.txt include in RESX maybe enough.
Steps to include a file created at build time into RESX:
Add TextFile1.txt to your project
Drop that file on {resourceFileName}.RESX (in design mode)
Check if {resourceFileName}.Designer.cs is updated with new property to access content of the new resource
In Solution explorer chose properties of your project and open "Build Events" tab
Add "date /t > ....\TextFile1.txt" to pre-build steps (will dump current date into "TextFile1.txt"
Build and use new resource variable i.e. Console.WriteLine(Resource1.TextFile1)
RESX sample content:
<data name="TextFile1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>textfile1.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>

resx is for storing static resource. Though there could be hack to change resx at run time, or during development time through some sort of scripting, however, both introduce too much complexity for little feature.
If you could clarify the use cases of such copyright string, readers might be able to provide more concrete suggestion. Say, do you use the string only for displaying in an About box? or assembly attribute?

Related

C# How to get the assembly of pending changes during check in policy

I want to raise the assembly version number of a project by using a check in policy. How can I get the assembly by using CheckedPendingChanges? I get the filename but I don't know how to extract generically the assembly out of it so that I can manipulate the version number and to check it out.
this.PendingCheckin.PendingChanges.CheckedPendingChanges
I'm open minded for other approaches to raise the version number of a project where some code changes are inside.
If you want to go with this approach, you could look for all .csproj (or whichever project format relevant to you) that contain that filename (you will need to care for relative vs absolute paths).
I would prefer the build system to be responsible for versions, triggered on specific paths to identify the project . I prefer an assembly version format like yyyy.MM.dd.revision. II have an example lying around for programatically stamping all assemblyinfo.cs files in a solution with a custom version through msbuild, which could be adapted to your needs. I will find it and post it when I reach home.

Unknown resource file entry starting with >>

In a Visual Studio resource file (.resx) for a C# project I've located strange entries which start with two greater-than signs (>>), e.g.:
<data name=">>$this.Type" xml:space="preserve">
<value>Framework.Forms.MyForm, Framework, Version=1.1.5127.0, Culture=neutral, PublicKeyToken=f4aaf1fba1062dc8</value>
</data>
These entries sort of reference various classes (MyForm, MyButton, etc.) in a custom framework library but in an outdated version.
Are these entries still valid? Should I change them so that they reference the current framework version? Do they have an impact on the project? Why does the name start with >>?
This is how .Net/Winforms stores text resources from Windows forms classes, if you have set "Localizable" to "True". This way, they are taken out of the .designer.cs code and are put into a resource file (one for every language supported) that can be switched, depending on the chosen language.
This is at least the one case I know, where resources appear that start with ">>".

Visual Studio C# Updating Localized Languages resx

I have multi Language Application, first it was made in English only,
than one time i added support for German and Russian version and send resx files for translation to mates.
But while they do translation, i added few more checkboxes in GUI
and now when i copy their files and load German \ Russian GUI version those not translated checkboxes do not shown in in those languages.
My Question is:
Is there a way to update localized .resx files to version of main, as i can see it:
in Localized files if controls are missing - they will display in default language.
For translation they used: Zeta Resource Editor
Found answer by myself :) Maybe someone need it:
In this free program (Zeta Resource Editor) You can Add group of existing resources from Your VS Project - just select them an add, Zeta Resource Editor will auto show which strings are missing, so you can easily find them.

How to automatically change which files are built depending on configuration - visual studio

I have a solution which is built for several customers, and I need to be able to specify different xml files for each customer. How can I do this automatically. I was thinking it might be done with different configurations, but can't seem to figure out how.
Any suggestions?
EDIT:
This is the code used for declaring the xml file right now:
protected readonly static string XML_PATH = #"Resources/xml/Description.xml";
And the way it is solved now is to manually copy the correct file to the Description.xml before building. This is of course error prone, and I would like to automate it, preferentially based on the configuration. I'm looking for a quick fix right now, as we unfortunately haven't got the time to refactor the code.
Build Configuration dependent config files are a tricky issue and there are multiple ways to solve it.
If you want to down the road you outlined, you would need to manually edit the *.csproj File and add a Conditional ItemGroup to include the correct xml file. The syntax below hasn't been checked, but something like this should do
<ItemGroup Condition="'${Configuration}' == 'DEBUG'">
<Content Include="blablabl.xml"/>
</ItemGroup>
I don't remember if Content was the right ItemGroup, but simply check what ItemGroup your current .xmls are in and use that.
Based on your reformulated question:
You could use conditional compilation (caveat: It's messy and not the right way to manage config files!):
protected readonly static string XML_PATH =
#if DEBUG
#"Resources/xml/Description.xml";
#else
#"Resources/xml/Description2.xml";
#endif
If you want to read up on better techniques for managing config files, this is worth a read.
Now, I now self-promotion is frowned upon, but in this case I hope it's ok as it sounds relevant to the question, and I don't gain anything from this.
Recently I wrote a couple of blog posts on how to target multiple environments/machines:
Targeting multiple environments and machines - part 1/2
Targeting multiple environments and machines – part 2/2
As I understand it, the problem in this case, is how do you automatically build the correct set of files without having to manually figure out which files belong to which customer/environment. The solution I propose in the blog posts, suggests the use of nAnt along with some extensions built on top. nAnt is the .NET versions of Ant, a build tool, which lets you generate e.g. xml files given a specific set of input files, allowing you for example to generate a customer specific web.config file.
In the following appSetting section of the web.config file, say you want to specify a different value for the CustomerName key for each customer:
<appSettings>
<add key="CustomerName" value="${CustomerName}"/>
</appSettings>
Instead of specifying a value for the CustomerName key, you define a property called CustomerName. Now, assuming we are using nAnt, you create another customer specific file with the following content:
<?xml version="1.0" encoding="utf-8"?>
<target xmlns="http://nant.sf.net/release/0.86-beta1/nant.xsd">
<property name="CustomerName" value="Acme Incorporated"/>
</target>
nAnt can then merge these two files and automatically build customer/environment specific files for you.
The solution I go through, allow you to automatically build environment and machine specific files, such as the web.config file, but also allow you to output static files such as license files or libraries, all depending on which environment/machine you are targeting. I also supply a sample Visual Studio 2010 solution that shows a very basic example on how to do it, which you can download here.
You can of course just go ahead and take a look at nAnt, but I thought I'd provide you with the option to use my solution.

How to modify Settings.Designer.cs at build time?

I have a C# class library project with some settings in Settings.settings. I need to be able to change these settings at build time based on the configuration (Debug, Release, etc.).
It's fairly straightforward to add a pre-build event to copy Settings.<configuration>.settings to Settings.settings, but as it turns out - this doesn't help! The settings are taken from Settings.Designer.cs which is generated from Settings.settings as soon as you save your changes (i.e. at code edit time).
Is there a way to regenerate Settings.Designer.cs from Settings.settings at compile time? Or is this the wrong way to go about modifying configuration settings?
The Settings.Designer.cs is generated by the IDE, not MSBuild. So, no, changing that file at compile time won't have any effect. You didn't document your question well enough to offer the best alternative, but it sure sounds like using a setting wasn't the correct choice.

Categories

Resources