Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Why is Application in System.Windows.Forms?

Tags:

c#

.net

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?

like image 641
Svish Avatar asked Nov 02 '09 09:11

Svish


2 Answers

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

like image 113
marc_s Avatar answered Oct 20 '22 17:10

marc_s


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.

like image 26
Joe Avatar answered Oct 20 '22 15:10

Joe