C#: Why is Application in System.Windows.Forms?
The Application
class in System.Windows.Forms
have a some properties that can be quite useful. For example:
- ProductName
- ProductVersion
- CompanyName
- ExecutablePath
- StartupPath
- Common开发者_如何学运维AppDataPath
- CommonAppDataRegistry
- UserAppDataPath
- UserAppDataRegistry
- LocalUserAppDataPath
Why are these in in a class in System.Windows.Forms
? What if I wanted to access the CommonAppDataPath
in a console application? Would I have to reference System.Windows.Forms.dll then, or is there an alternative for console applications?
For the paths, you can also look at the Environment
class in .NET:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
and there's a whole slew of "special" folders you can ask for.
The CompanyName and other options are pulled from the "AssemblyInfo.cs" in your "Properties" folder by default - if you have such a file, you can also access those yourself in code.
Hm, with those paths you have to append the company name and product name and such yourself though...
Yes, that's what the System.Windows.Forms assembly is doing for you. If you don't have a Winforms app, you'll have to do that yourself, that's true.
Marc
Information such as "ProductName" comes from the main assembly in a WinForms application. There is no "main" assembly in an ASP.NET application.
If you are running under a service account without a profile (which may often be the case for an ASP.NET app), there will be no UserAppDataPath - in fact attempting to dereference the UserAppDataPath property will throw an exception.
For these reasons, it would not make sense to expose this information to an ASP.NET app.
Quoted from MSDN System.Windows.Forms namespace documentation:
The System.Windows.Forms namespace contains classes for creating Windows-based applications that take full advantage of the rich user interface features available in the Microsoft Windows operating system.
The only real reason I can see the Application class being placed in the System.Windows.Forms namespace is because it handles Windows Messages which are usually posted by Form controls.
Application class launches win32 message loop which is tightly related to windows forms.
You could get the info you're looking for using the Assembly namespace
精彩评论