How to get exe application name and version in C# Compact Framework

你说的曾经没有我的故事 提交于 2019-11-28 06:50:50

Getting the app name:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

Getting the version:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

You might want to use GetCallingAssembly() or getting the assembly by type (e.g. typeof(Program).Assembly) if your DLL is trying to get the EXE version and you don't have immediate access to it.

EDIT

If you have a DLL and you need the name of the executable you have a few options, depending on the use case. You can get the Assembly from a type contained in the EXE assembly, but since it would be rare for the DLL to reference the EXE, it requires the EXE pass in an object of that type.

Version GetAssemblyVersionFromObjectType(object o)
{
    o.GetType().Assembly.GetName().Version;
}

You could also do a little bit of an end-run like this:

[DllImport("coredll.dll", SetLastError = true)]
private static extern int GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, int nSize);

...

var name = new StringBuilder(1024);
GetModuleFileName(IntPtr.Zero, name, 1024);
var version = Assembly.LoadFrom(name.ToString()).GetName().Version;
Rohan S D

System.Reflection.Assembly.GetEntryAssembly().GetName().Version;

This function will give version of Application from where other libraries are loaded.

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

This will give version of current library. If you call this in Application library you will get application version and if call this in a DLL then will get that DLL version.

So in my opinion System.Reflection.Assembly.GetEntryAssembly().GetName().Version; is currect function to use.

If you want to create a component (.dll) use in to another app reference for fetch the main app name and version, you can use this way:

Getting the main app name:

AppDomain.CurrentDomain.DomainManager.EntryAssembly.GetName().Name;

Getting the version:

AppDomain.CurrentDomain.DomainManager.EntryAssembly.GetName().Version.ToString();    

Getting the FullName (contain: app name, app version, culture, publicKeyToken):

AppDomain.CurrentDomain.DomainManager.EntryAssembly.FullName;

But in this solution there is a problem, this that, is dependent on the host and if it runs directly from the executable file, the error will occur. Therefore the following elections:

Getting the main app name:

string appName = AppDomain.CurrentDomain.FriendlyName;
appName = appName.Substring(0, appName.IndexOf('.'));

Getting the version:

System.Windows.Forms.Application.ProductVersion;

Can you try using FileInfo Class FileVersionInfo. Hope this will help..

You can also try Win32 API like GetModuleFile()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!