I have a C# WinForms app with an About box. I am putting the version number in the about box using:
FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location)
.FileVersion
This ends up giving me the Subversion revision number from which the executable was built.
I would also like to get the date of the build into the About box. I tried:
File.GetLastWriteTime(Assembly.GetEntryAssembly().Location)
But that gives me the write date of the executable, which only corresponds to the date when the app was installed (we are using ClickOnce) not built.
How can I get the build date?
If you use automatic versioning, you can convert the last two bits of the version number into a build date: MSDN
We're using this very similiar piece of code:
DateTime buildDate = new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;
and I'm pretty sure it doesn't change when installing from ClickOnce.. If I'm wrong please correct me!
You could change your assembly versioning to encode the date, but that would probably mean losing your subversion revision information which is arguably more useful.
This should work: write the current date/time into a .cs file as a pre-build task like so:
[assembly: AssemblyCreated(CreatedDate = new DateTime(...))]
You could use a batch file, PowerShell script or executable for that.
Include the file in your project (build action: compile) and include the custom attribute:
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class AssemblyCreatedAttribute : Attribute
{
public DateTime CreatedDate { get; set; }
}
On application start you can use reflection to get the custom attribute from the assembly for display in the about page.
The only way i was able to do it in C/C++ was to actually have a post-build process do a search and replace of a special string allocated as a "static const" in the binary.
There might be an easier way in c# though.
I don't think a standard way of doing it exists, but you can roll something up yourself. Create a custom assembly-level attribute, give it a cool name like 'AssemblyDateAttribute'. Let it take a string that you can parse into a DateTime in the constructor that is accessible via a property.
As part of a build process, create a new file with only the attribute being applied to the assembly. (Make it look like AssemblyInfo.cs) and then include that in your build input.
Then in your about box, search your assembly for instances of this attribute and display the date value in your box.
In Visual Studio projects there is a file AssemblyInfo.cs, but you can use any other .cs file. Look at the attribute AssemblyVersion:
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.*")]
Now you can calculate the build date by using the Version.Build property. The value that Version.Build returns is the number of days since: 2000/1/1
Assembly.GetExecutingAssembly().GetName().Version.ToString();
From my own C# code.
Related
I don´t know how i should explain what i want to do.
In my Project are basicly 3 Sub-Projects.
First Project: Holds all Properties and Localizations.
Second Project is an Console Project, using the Properties from the First Project and some Localized Strings.
Third Project is an Windows Forms Project, using all Properties and Localized Strings from the First Project.
The Localization is working without trouble, i can start both Projects and when i change the Culture, it changes the Strings.
But: I have to Write the whole Path to the String everywhere.
Example:
Label.Text = Config.Localization.MyString
With the Settings, i can use an "Shortcut" by setting it to an new variable:
private Settings settings = Config.Properties.Settings.Default;
But with the Localization i cant to that to shorten it up.
Try´d the following:
private Type locals = CodepackConfig.Localization;
Label.Text = locals.MyString
Does somebody have hint for me how i can set a Variable to "Config.Localization"? And yes, i just code C# for 1 Month now, looks like a stupidly easy error :)
Check out the using directive's documentation.
I assume you're interested in something like
using Localization = Config.Properties.Localization;
1: https://msdn.microsoft.com/en-us/library/sf0df423.aspx?f=255&MSPPError=-2147217396
So, in my ASP.NET C# code base I have possibly hundreds of bits of code like this:
Response.Redirect("something.aspx?Error=" + ex.Message);
I want to automatically add an argument to all of these method calls to add 'true' as the second parameter to this method, like this:
Response.Redirect("sometihng.aspx?Error=" + ex.Message, true);
I have Visual Studio 2010 and the latest version of Resharper at my disposal.
I tried using the 'Search with Pattern' feature in Resharper (VS menu -> ReSharper -> Find -> Search with Pattern) to see if this would automatically refactor my codebase, but I'm not sure exactly how or if it works. Here's what I tried:
On the right-hand side, I created an 'Argument' placeholder called 'anyString', in the hope that this would find and replace all invocations of Response.Redirect that have a string in the first argument, but this found no matches in my code-base.
Any ideas on how I might solve this without resorting to manually changing all references?
As per the Jetbrains Resharper documentation on 'Searching a Code with Pattern':
Pay attention, that when you use a placeholder, its name should be
enclosed with dollar signs (use the syntax $xx$, where xx represents
placeholder name),whereas when you create a new placeholder, you
should omit these special symbols.
Therefore, I was on the right track. Also for the placeholder I just need '$anyString$' and it will find all invocations of the method, even if they are made up multiple string objects (e.g. string literals and string objects). So this is how it would look:
The 'anyString' placeholder pattern was created by performing the following steps:
1) Click 'Add Placeholder' -> Argument
2) Give it a name, e.g. 'anyString'
For my case, I also checked the 'Limit minimal number of arguments' and selected 1, and I also checked the 'Maximal' box and set that to 1 also.
The 'Save' button is also useful if you intend on reusing the pattern again.
I am using whitestaruml 5.4
with class diagram, i can't seem to be able to type this attribute
ServiceCategory: Dictionary<string,List<service>>
But Dictionary<string,List<service>> is a valid type for C#, how to get over this?
I suppose you are getting a lexical error on '<' as currently the input analyzer in WhiteStarUML does not accept embedded delimiters ( <[( style ) in the entity names to better handle matching opening/closing at lexer level. In the future this may get more sophisticated but currently the best way to skip parsing is to select the attribute in Model Explorer and fill the "Type" field in Property Inspector. As no parsing happens there it will be accepted. Just make sure not to modify the attribute in the Model View on the Diagram again.
It's June 2022 - still not fixed. Very disappointing.
When directly entering attributes, I just put a placeholder type (e.g., 'foo'). After which, you can go click on the attribute on the right pane and go down to the Editors / type field and replace it with what you want.
I use a lot of map<string,string> and have to do this often. If you try to directly edit a method and its arguments later, it will block you again and you'll have to repeat the procedure - but it can be done to make your diagrams correct. It's just a PITA.
In WinForms I have an AssemblVersion
[assembly: AssemblyVersion("01.01.01.002")]
However when the splash screen comes up it completely ignores the zeros showing:
1.1.1.2
as the version which is very inconvenient since later I will actually want to have an assembly version
[assembly: AssemblyVersion("01.01.01.200")]
Is there a way to avoid this or do I Have to add some number at the beginning of last part of the version like so:
[assembly: AssemblyVersion("01.01.01.102")]
The AssemblyVersion attribute stores it's information as a Version object. The components of the Version struct are integers, and are treated as such. So 1.2.3.4 == 1.02.003.004 but 1.2.3.4 != 1.2.3.400
You can use the AssemblyInformationalVersionAttribute to provide aditional, arbitrarily formatted information about your product, as it's information is stored as a string, rather than a Version. So you can do:
[assembly: AssemblyVersion("1.1.1.102")]
[assembly: AssemblyInformationalVersion("v.01 alpha")]
Or whatever you like
I have a C# application that I need to convert to support English and Spanish, is there a semi easy way to add that in and be able to add other languages later on?
Yes! It's called resource (.resx) files. What you do is this:
Change the Localizable property of your localizable forms to true. This will make the designer fetch text and other properties from the .resx files instead of hard-coding them.
Create your program in one language, let's say English.
Next, change all your forms to another language like so:
Change the Language property of the form to the other language, let's say Spanish.
Change the text on all your controls. The designer will automatically generate a new .resx file for the language.
Swap back and forth as needed during development.
When publishing, go into your Assembly Settings and change the language. You can also change the language in code, I think.
And voilà! You're done!
You mark all your forms and controls as localizable. This will put all UI related text (labels etc.) in resource files. If you need to create strings in code then you use string resource files and look up the string by the resource key (e.g StringResource.Get("My_Message")). Then you can use a tool to translate all your resources. Typically you create a localized .dll for each language. We use Passolo for that but there are other tools around.
You can make a multilingual application in two ways:
By making the application Localizable, so when the user changes the culture of the device, the application will switch automatically to culture's UI if you added this language already to the supported languages in the application.
You can perform this by setting each form's Localizable property on the project to Localizable, and then changing the UI to the new culture.
By making a language option and a resource file (.resx) for each added language in your application, and depending on the selected language, you can load the images or the strings from selected language's resource file.
Without installing any 3rd party tool, APIs, or dll objects, I am able to utilize the App_LocalResources. Although I still use Google Translate for the words and sentences to be translated and copy and paste it to the file as you can see in one of the screenshots below (or you can have a person translator and type manually to add). In your Project folder (using MS Visual Studio as editor), add an App_LocalResources folder and create the English and other language (resx file). In my case, it's Spanish (es-ES) translation. See screenshot below.
Next, on your aspx, add the meta tags (meta:resourcekey) that will match in the App_LocalResources. One for English and another to the Spanish file. See screenshots below:
Spanish: (filename.aspx.es-ES.resx)
English: (filename.aspx.resx)
.
Then create a link on your masterpage file with a querystring that will switch the page translation and will be available on all pages:
<%--ENGLISH/SPANISH VERSION BUTTON--%>
<asp:HyperLink ID="eng_ver" runat="server" Text="English" Font-Underline="false"></asp:HyperLink> |
<asp:HyperLink ID="spa_ver" runat="server" Text="Español" Font-Underline="false"></asp:HyperLink>
<%--ENGLISH/SPANISH VERSION BUTTON--%>
.
On your masterpage code behind, create a dynamic link to the Hyperlink tags:
////LOCALIZATION
string thispage = Request.Url.AbsolutePath;
eng_ver.NavigateUrl = thispage;
spa_ver.NavigateUrl = thispage + "?ver=es-ES";
////LOCALIZATION
.
Now, on your page files' code behind, you can set a session variable to make all links or redirections to stick to the desired translation by always adding a querystring to urls.
On PageLoad:
///'LOCALIZATION
//dynamic querystring; add this to urls ---> ?" + Session["add2url"]
{
if (Session["version"] != null)
{
Session["add2url"] = "?ver=" + Session["version"]; //SPANISH version
}
else
{
Session["add2url"] = ""; // ENGLISH as default
}
}
///'LOCALIZATION
.
On Click Events sample:
protected void btnBack_Click(object sender, EventArgs e)
{
Session["FileName.aspx"] = null;
Response.Redirect("FileName.aspx" + Session["add2url"]);
}
I hope my descriptions were easy enough.