Search code examples
vb.netstartup

Getting Application.CommonAppDataPath and UserAppDataPath in ApplicationEvents Startup


I would like to set the default location for some files on Application Startup, but in the ApplicationEvents' MyApplication_Startup sub I can't seem to get the Application.CommonAppDataPath and Application.UserAppDataPath variables.

Is there any way around this? Without hard coding of course.


Solution

  • Application in the main part of your project (in your own forms, classes, etc.) refers to the System.Windows.Forms.Application class. However under the MyApplication class (or more specifically the My namespace) it refers to <your namespace>.My.Application (which is the same as My.Application).

    This happens because the framework's classes and objects are shadowed by your project's local objects. In this case the framework's Application class is shadowed by the project's My.Application property.

    You simply need to add the namespace in the beginning:

    System.Windows.Forms.Application.CommonAppDataPath
    System.Windows.Forms.Application.UserAppDataPath