在 ApplicationEvents Startup 中获取 Application.CommonAppDataPath 和 UserAppDataPath

Getting Application.CommonAppDataPath and UserAppDataPath in ApplicationEvents Startup

我想在应用程序启动时为某些文件设置默认位置,但在 ApplicationEvents 的 MyApplication_Startup 子中我似乎无法获得 Application.CommonAppDataPathApplication.UserAppDataPath变量。

有什么办法解决这个问题吗?当然没有硬编码。

Application 在您项目的主要部分(在您自己的表格中,classes 等)指的是 System.Windows.Forms.Application class。 但是 MyApplication class(或更具体地说是 My 命名空间)下它指的是 <your namespace>.My.Application(与 My.Application).

发生这种情况是因为框架的 classes 和对象被项目的本地对象遮蔽了。在这种情况下,框架的 Application class 被项目的 My.Application 属性.

隐藏

您只需在开头添加命名空间:

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