简体   繁体   中英

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
  • CommonAppDataPath
  • 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.

应用程序类启动与Windows窗体紧密相关的win32消息循环。

您可以使用Assembly命名空间获取您正在查找的信息

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM