Reputation: 5752
In trying to obtain .exe version number and name I see that we could use different ways such as:
AppDomain.CurrentDomain.FriendlyName //shows: myapp.exe
or use
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name //shows myapp
Both classes produce the "almost: the same name, but my question is:
What is the difference between these two? Why use one over the other (forget performance)?
Thanks.
Upvotes: 0
Views: 566
Reputation: 7187
If you are trying to get information about the executable, you can also go a little bit differently and try this.
Note: This example retrieves information about the currently executing assembly. But this approach can gather information for any executable (not only yours).
Note: Json serialization is used here only to be able to show what information is available through the FileVersionInfo
class.
string executablePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(executablePath);
Console.WriteLine(JsonConvert.SerializeObject(fileVersionInfo, Newtonsoft.Json.Formatting.Indented));
The output is like the following for my very simple console application:
{
"Comments": "",
"CompanyName": "",
"FileBuildPart": 0,
"FileDescription": "ConsoleApplication1",
"FileMajorPart": 2,
"FileMinorPart": 1,
"FileName": "C:\\Projects\\.....\\ConsoleApplication1.exe",
"FilePrivatePart": 0,
"FileVersion": "2.1.0.0",
"InternalName": "ConsoleApplication1.exe",
"IsDebug": false,
"IsPatched": false,
"IsPrivateBuild": false,
"IsPreRelease": false,
"IsSpecialBuild": false,
"Language": "Language Neutral",
"LegalCopyright": "Copyright © 2019",
"LegalTrademarks": "",
"OriginalFilename": "ConsoleApplication1.exe",
"PrivateBuild": "",
"ProductBuildPart": 0,
"ProductMajorPart": 2,
"ProductMinorPart": 1,
"ProductName": "ConsoleApplication1",
"ProductPrivatePart": 0,
"ProductVersion": "2.1.0.0",
"SpecialBuild": ""
}
Upvotes: 1
Reputation: 19340
Assembly is more physical thing (if I can say that), while App Domain is totally in-memory only. App domain is where your app basically starts and interacts with the framework, and it has multiple loaded assemblies. If your main assembly happen to be a startup assembly in app domain, you might get same name for assembly and domain. In your particular case you started app with exe and your current domain is app.exe
. But it is not assembly name, which is does not have a file extension. Assembly has its own version vs file version. They can match or not. If you have application components, you can place this in any of them Assembly.GetCurrentAssembly()
, and it will return you something else. But the main thing is, AppDomain <> Assembly
. These are 2 different beasts.
"Why use one over the other (forget performance)?" Depends what you are trying to do. You use both things any time application is running. But if your goal is just to get your application information, such as name, file version, assembly version - use assembly
Upvotes: 2