3

我维护一个 VB6 应用程序,将其数据(访问文件)存储在应用程序文件夹的子文件夹中。因此,当用户将应用程序安装到默认位置时C:\Program Files\MyApp,数据最终会出现在虚拟存储中。我现在得到了一项艰巨的任务,将 1000 条左右的 App.Path 语句转换为不会有任何安全问题的 Data 文件夹的路径。有人可以通过指出一些资源的方式来帮助我,这些资源将向我提供如何处理Users\< Username>\AppData\Local\文件夹中的数据吗?
我应该使用本地 AppData 文件夹,还是让用户选择合适的位置?

4

4 回答 4

2

我会使用本地 AppData 文件夹:

Dim sAppData As String

sAppData = Environ("USERPROFILE") & "\AppData"
于 2010-12-19T23:30:38.820 回答
2

好吧,如果我们假设几件事:

  • 这只是 Vista 或更高版本的问题。
  • 您的数据位于App.Path 下的子文件夹中。

...我有一种可以使用的解决方法。

请注意,当我有一堆旧的 VB6 程序被编写为使用 App.Path 下文件夹中的数据时,我只使用这个技巧,这是一种快速而肮脏的 hack,可以让很多程序快速运行。我几乎总是添加或更新应用程序清单,主要是为了防止虚拟化。


我所做的更改是:

  • 向 Sub Main 添加一些代码(如果程序还没有 Sub Main,则插入一个 Sub Main)。
  • 将两个 Class 模块(由新的 Sub Main 代码调用)添加到项目中。

部署新程序时,在 Vista 或更高版本上,该程序需要以管理员身份运行一次。在我的加载项代码的最新版本中,该程序将提示用户在需要时重新运行提升它,并且在确定时它会这样做并自行终止。

在第一次运行之后,一切都应该是正常的,运行起来就像在 Win2K、XP 等下一样。


此需要提升的启动代码提供了 PathLinks 类所需的 App.Path 子文件夹的列表。

Pathlinks 在 Public 特殊文件夹下创建一个程序文件夹,然后为该文件夹下的数据创建匹配的子文件夹。接下来,它将这些 App.Path 子文件夹中的所有文件和子文件夹移动到新位置。最后,它在 App.Path 下创建指向新文件夹的符号链接。

如果在 Vista 之前的 Windows 下运行,PathLinks 只会创建任何不存在的 App.Path 子文件夹列表(在 App.Path 下)(即通过安装)。

从这里开始,程序将在新位置找到文件,根本不需要对其使用 App.Path 进行任何更改。


两个加载项类之一是装饰性的,您可以将其删除。它只是让启动代码调用 TaskDialog 而不是使用 MsgBox 调用。

请务必向下滚动查看PathLinks - Tame App.Path Under Vista+上发布的最新版本

请注意,示例项目在执行“首次提升”技巧时会跳过运行应用程序。它只是在重新定位数据并对其进行符号链接后在 Sub Main 中执行 Exit Sub。

于 2010-12-21T00:15:28.523 回答
0

可以在这里找到有用的讨论

于 2010-12-27T09:43:14.007 回答
0

我不知道是否还有人(因为我仍在寻找它)正在寻找这个。但我在这个页面上找到了一个很好的答案 http://www.vbforums.com/showthread.php?564256-Classic-VB-Where-should-I-store-the-files-that-my-program-uses-creates

我使用此代码获取正确的文件夹路径。

'this peace of code must be on the top of jour module in the declaration part.
Public Enum eSpecialFolders
  SpecialFolder_Documents = &H5         'the Documents folder for the current Windows user
  SpecialFolder_Favorites = &H6         'for the Favorites folder
  SpecialFolder_Videos = &HD            'For the Video's folder
  SpecialFolder_Desktop = &H10          'for the desktop folder
  SpecialFolder_AppData = &H1A          'for the current Windows user, on any computer on the network [Windows 98 or later]
  SpecialFolder_LocalAppData = &H1C     'for the current Windows user, on this computer only [Windows 2000 or later]
  SpecialFolder_CommonAppData = &H23    'for all Windows users on this computer [Windows 2000 or later]
  SpecialFolder_Windows = &H24          'for the windows folder
  SpecialFolder_System32 = &H25         'For the windows system32 folder
  SpecialFolder_Pictures = &H27         'for the picture folder
  SpecialFolder_User = &H28             'for user folder C:\Users\sidxxxx
End Enum

'this can be placed before or after other functions
Public Function SpecialFolder(pFolder As eSpecialFolders) As String
'Returns the path to the specified special folder (AppData etc)

Dim objShell  As Object
Dim objFolder As Object

  Set objShell = CreateObject("Shell.Application")
  Set objFolder = objShell.namespace(CLng(pFolder))

  If (Not objFolder Is Nothing) Then SpecialFolder = objFolder.Self.Path

  Set objFolder = Nothing
  Set objShell = Nothing

  If SpecialFolder = "" Then Err.Raise 513, "SpecialFolder", "The folder path could not be detected"

End Function

我在同一个模块中使用了它,就像在另一个函数中一样。

文件名 = SpecialFolder(SpecialFolder_AppData) & "\Log\" & "log.log"

这将设置 FileName = "C:\Users\username\AppData\Roaming\Log\log.log"

亲切的问候

于 2020-04-02T14:32:26.783 回答