Application.ProductVersion is not showing the incremental version. can anybody help me how to perform this, using C# ?
You can have build and revision incremented for you but not major and minor.
Simply substitute
[assembly: AssemblyVersion("1.0.0.0")]
with
[assembly: AssemblyVersion("1.0.*")]
in the AssemblyInfo.cs
Have you tried grabbing the Assembly's version?
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
Perhaps this is what you are looking for.
Also check out this other SO post - I think this is what you are looking for.
Automatically update version number
Below is a second link to a .Net add-in that automatically increments the:
Major
Minor
Build
Revision
http://testdox.wordpress.com/versionupdater/
I have found that it works well to simply display the date of the last build using the following wherever a product version is needed:
System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString("yyyy.MM.dd.HHMM")
Rather than attempting to get the version from something like the following:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
object[] attributes = assembly.GetCustomAttributes(typeof(System.Reflection.AssemblyFileVersionAttribute), false);
object attribute = null;
if (attributes.Length > 0)
{
attribute = attributes[0] as System.Reflection.AssemblyFileVersionAttribute;
}
Related
Is there a way to find out the assembly name at design-time (i.e. not using reflection or runtime APIs such as System.Reflection.Assembly.GetEntryAssembly) from within Visual Studio?
The scenario requires a tool to get the assembly name that a Visual Studio project will eventually compile into.
This is like parsing the AssemblyName property of the .csproj - I am wondering if there are any APIs that can give this information reliably.
Please do not respond back with runtime APIs that use reflection - there is no assembly file present at the time I need the assembly name - just the metadata of the assembly in the csproj file.
if you are calling the tool via a post/pre-build event, this data is very easy to access.
Just go to the "project properties->Build Events" tab, then select either "edit pre-build" or "edit post-build", depending on when you want the tool to run. This should bring up an edit window with the ever helpful "Macros >>" button. Press this and you will be given a heap of macros to use and should be pretty much everything you need.
The "API" you could use is LINQ to XML after all the .csproj file is just xml. (and you can get the location of the .csproj file if you need from the solution file which for some reason is not XML but can be easily parsed)
You can use "TargetName" available in Macros for Post-build events. It will give you the assembly name for your project.
After a quick run through MSDN I found this article which might be a good start for some further research:
Accessing Project Type Specific Project, Project Item, and Configuration Properties
I think you will need to write some regular expression that will give you the value of "AssemblyTitle" attribute in AssemblyInfo.cs file.
Something like this:
public class Assembly
{
public static string GetTitle (string fileFullName) {
var contents = File.ReadAllText (fileFullName); //may raise exception if file doesn't exist
//regex string is: AssemblyTitle\x20*\(\x20*"(?<Title>.*)"\x20*\)
//loading from settings because it is annoying to type it in editor
var reg = new Regex (Settings.Default.Expression);
var match = reg.Match (contents);
var titleGroup = match.Groups["Title"];
return (match.Success && titleGroup.Success) ? titleGroup.Value : String.Empty;
}
}
I am able to get the file version information through the namespace FileVersionInfo in C#. In FileVersionInfo, if file version is empty then we aren't able to get other information like product version, company name etc. I can see the file version information when we right click the file and select the details tab in Properties window but i can't get the value through C# code. So Can I get file version information without using FileVersionInfo? Are there any other ways to get the file version information.
Try this :-
Assembly assembly = Assembly.LoadFrom(#"D:\exePath\yourexe.exe");
Version ver = assembly.GetName().Version;
To get the file version you have to get the Version set by the AssemblyFileVersionAttribute, for example like this:
[assembly: AssemblyFileVersion("3.2.10.0")]
Here is the code of an extension method to read the attribute:
public static Version GetFileVersion(this Assembly assembly)
{
AssemblyFileVersionAttribute attribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
return attribute == null ? null : new Version(attribute.Version);
}
To load the assembly use the Assembly.LoadFrom method as mentioned before.
Our build process updates the forth digit (buildnumber or revision) of the version number on every build.
I'm using the following code to try and get the version so that it can be displayed on the splash screen and about page.
var version = Assembly.GetEntryAssembly().GetName().Version;
var programName = "Version: " + version;
How come the 4th digit in the version number is always zero, and what can I change to get it to property reflect the build number?
Any assembly has two metadatas: the assembly version and the file version. The assembly version I don't know exactly how to take it. It seems that there is an AssemblyVersionAttribute but when I try to search for it, a null reference is returned.
But the File version you can take using this code:
public static Version GetAssemblyVersion (Assembly asm)
{
var attribute = Attribute.GetCustomAttribute(asm, typeof(AssemblyFileVersionAttribute), true) as AssemblyFileVersionAttribute;
return new Version(attribute.Version);
}
Generally, I use to synchronize so that file version = assembly version every time so I can grab assembly version using this code. Third party libraries, from my experience I can tell that are following this pattern also.
I have a program that I have written and am trying to create an about box for. I recently updated my program's product version to 1.00.0003, and I want this to be reflected in the about window.
The default setup of the aboutBox shows a value of 1.0.0.0, which is the assembly version, not the product version. I have since been scouring the Internet to find how to get the product version to be shown. I have tried all of these:
{
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fileVersionInfo.ProductVersion;
Debug.WriteLine(version);
Debug.WriteLine(assembly.GetName().Version);
string v = VersionNumber;
Debug.WriteLine(v);
Debug.WriteLine( fileVersionInfo.FileVersion);
Debug.WriteLine(Application.ProductVersion);
Debug.WriteLine(AssemblyProductVersion);
Assembly assembly2 = Assembly.GetEntryAssembly();
FileVersionInfo fileVersionInfo2 = FileVersionInfo.GetVersionInfo(assembly.Location);
string version2 = fileVersionInfo2.ProductVersion;
Debug.WriteLine(version2);
Debug.WriteLine(assembly2.GetName().Version);
return version;
}
private string _ourVersion = "Version: v";
private string VersionNumber
{
get
{
System.Reflection.Assembly _assemblyInfo =
System.Reflection.Assembly.GetExecutingAssembly();
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
_ourVersion += ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
else
{
if (_assemblyInfo != null)
_ourVersion += _assemblyInfo.GetName().Version.ToString();
}
return _ourVersion;
}
}
private static string AssemblyProductVersion
{
get
{
object[] attributes = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
return attributes.Length == 0 ?
"" :
((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;
}
}
Every single one of these returns 1.0.0.0 (yes, I did look for their output in the console, not what was actually displayed), instead 1.00.0003 like I need. The product version is set in the General Information tab of the InstallShield setup. When it is installed, going to Programs and Features shows a Product Version of 1.00.0003, so I cannot figure out why this is so hard to programmatically retrieve this value. Any ideas?
Your product version should match the assembly version - have a look at How to make Product Version property match executable's version number automatically
The version 1.00.0003 you want to retrieve is the version of the installer of your product. To get this version programmatically you need to inspect the installer (MSI file), not the installed files. I'm not sure that is what you want to do but there is a answer to the question Checking ProductVersion of an MSI programatically that explains how to do that.
If you want your executable files to contain the same version number you need to store the version number in some way either using a .NET attribute like AssemblyFileVersion or a Windows VERSIONINFO resource.
InstallShield allows you to specify the product version on the command line. This allows you to store your product version in a single file and then use that as the source of both the product version embedded in your installer as well as AssemblyFileVersion of your assemblies.
If only the installer knows about this version information, the only place you could retrieve it from would be the registry.
Uninstall Registry Key:
The following installer properties give the values written under the registry key:
VersionMinor Derived from ProductVersion property
VersionMajor Derived from ProductVersion property
Version Derived from ProductVersion property
But I'd go with #devdigital's (implied) suggestion - you ought to have one of the assembly versions actually matching your installer version.
I am trying to get the file version using C#:
string file = #"C:\somefile.dll";
Console.WriteLine(FileVersionInfo.GetVersionInfo(file).FileVersion);
For most files this is fine, however for some i receive results that are different than the ones presented in the Windows file explorer.
See the attached image: the file version presented in windows is "0.0.0.0", however the one i get using FileVersion property is "000.000.000.000".
I've tried using different versions of the .NET (2, 3.5, 4) which give the same results.
Anyone else experienced this issue?
Thanks
Lior
It seems Windows Explorer are stripping leading 0s of the version parts.
Try creating an assembly with FileVersion 001.001.001.001, it will show as 1.1.1.1 in explorer. But your code would return the actual value (001.001.001.001).
EDIT:
Explorer will return 001.001.001.001 as ProductVersion, but only if AssemblyInformationalVersion isn't set, in which case it would return that as ProductVersion.
The reason is, in WIN32 API (and the file metadata), product versions are defined as string but file versions are defined as integer while in .NET, all of them are defined as integer.
If you use reflector and inspect FileVersionInfo class, you can see that they are loaded differently:
this.productVersion = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, format, new object[] { codepage, "ProductVersion" }))
But:
this.fileMajor = HIWORD(fixedFileInfo.dwFileVersionMS);
this.fileMinor = LOWORD(fixedFileInfo.dwFileVersionMS);
this.fileBuild = HIWORD(fixedFileInfo.dwFileVersionLS);
this.filePrivate = LOWORD(fixedFileInfo.dwFileVersionLS);