tags:

views:

277

answers:

5

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?

+6  A: 

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

marc_s
+1, sounds good to me, and all the useful stuff is available in any .net application, not limited to winforms
JL
Hm, with those paths you have to append the company name and product name and such yourself though...
Svish
@Svish: I don't understand what you mean - those are just the default "special folders" that Windows keeps internally - your user's AppData directory or stuff like that.
marc_s
For example: When I print out `Application.UserAppDataPath`, I get *C:\Users\userName\AppData\Roaming\CompanyName\ProductName\ProductVersion*. But when I print out Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), I get only *C:\Users\userName\AppData\Roaming*.
Svish
How would you access the stuff in AssemblyInfo.cs? It has no class... (hehe...)
Svish
That's where System.Windows.Forms gets that information from, that's what I'm saying
marc_s
The AssemblyInfo class is in the System.Reflection namespace. You would need to create a new Assembly class and then use the GetCustomAttributes method to retrieve the values.
James
@James: Aha, so some reflection is required to get those then.
Svish
@Svish, yup I would imagine the Application class is simply wrapping this up in the static properties.
James
A: 

Application class launches win32 message loop which is tightly related to windows forms.

Vitaliy Liptchinsky
+1  A: 

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.

James
Yeah, that is true. I guess someone at Microsoft messed up regarding the single responsibility principle or something then... :p
Svish
Wouldn't be the first time either! :P
James
A: 

You could get the info you're looking for using the Assembly namespace

Raj
Do you have an example?
Svish
+2  A: 

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.

Joe
Good points. Didn't think about the ASP.NET side of things :)
Svish