C# how to get dll assembly information? - c#

If we want to get the version number of a dll we could use,
Assembly assembly = Assembly.LoadFrom("System.Xml");
Version ver = assembly.GetName().Version;
How to get the other information (like AssemblyTitle, AssemblyProduct, etc..) ?

You can use
var attribute = assembly.GetCustomAttribute<AssemblyTitleAttribute>();
var title = attribute.Title;
to get the attribute. And same solution for AssemblyProductAttribute.

Related

Can I able to get file version information of an exe file without using FileVersionInfo?

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.

Getting the Version of my C# app?

I am working on desktop application. I have create a setup.
Ex. My Application. Version is 1.0.0.
I want to get the current version of my desktop application which is 1.0.0. I have tried by using Application.ProductVersion but it provides the version of my controls. (I am using DevExpress Control15.2.7, so it provides the current version as 15.2.7).
How can I get the current version of the installed application? I want to compare it to the installed version to provide a "New Version Available" functionality for my product.
The info you are looking for is in AssemblyInfo.cs.
To access the info written in there at runtime you can use the System.Reflection.Assembly.
Use System.Reflection.Assembly.GetExecutingAssembly() to get the assembly (that this line of code is in) or use System.Reflection.Assembly.GetEntryAssembly() to get the assembly your project started with (most likely this is your app).
In multi-project solutions this is something to keep in mind!
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString()
// returns 1.0.0.0
Corresponding AssemblyInfo.cs:
Corresponding EXE-properties:
This may be important when working with InstallShield (see comments) !
System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var fieVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly .Location);
var version = fieVersionInfo.FileVersion;
Another approach, which is basically the same as the accepted answer, is:
Version appVersion = Assembly.GetExecutingAssembly().GetName().Version;
versionLabel.Text = "v" + appVersion.Major + "." + appVersion.Minor + "." + appVersion.Build + ".";
Get the version of a specific assembly:
private const string AssemblyName = "MyAssembly"; // Name of your assembly
public Version GetVersion()
{
// Get all the assemblies currently loaded in the application domain.
Assembly[] assemblies = Thread.GetDomain().GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
if (string.Compare(assemblies[i].GetName().Name, AssemblyName) == 0)
{
return assemblies[i].GetName().Version;
}
}
return Assembly.GetExecutingAssembly().GetName().Version; // return current version assembly or return null;
}

How to get buildnumber / revision from Assembly? (4th digit)

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.

C# Winform runtime load Windows Forms Controller Library

I wanted to load a dll file runtime what contains Windows Forms Controller Library in C# Winform app.
I need help.
Thanks for help:
Horbert
Please use the below code
using System.Reflection;
Assembly dll= Assembly.Load("DALL"); //DALL name of your assembly
Type MyLoadClass = dll.GetType("DALL.LoadClass"); // name of your class
object obj = Activator.CreateInstance(MyLoadClass)
string assemblyPath = #"D:/MyAssembly.dll";
Assembly assembly = Assembly.LoadFrom(assemblyPath);
or using Reflection.
string assemblyPath = #"D:/MyAssembly.dll";
Assembly MyDll = Assembly.Load(assemblyPath);
Type MyLoadClass = MyDll.GetType("MyDll.MyClass");
object obj = Activator.CreateInstance(MyLoadClass);

How do I programmatically get the product (not assembly) version of a running program using C#?

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.

Categories