找到Windows用户的“真实”应用数据文件夹? [英] Finding a Windows user's "true" application data folder?

查看:308
本文介绍了找到Windows用户的“真实”应用数据文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Delphi 6应用程序,像大多数Windows应用程序一样,将数据读取/写入用户的本地应用程序数据文件夹。我使用下面的代码来确定该文件夹。到目前为止,该代码对我的大多数用户都有效。我遇到了本地应用程序数据不在预期文件夹中的用户:

  C:\Users\Bob\ AppData \Roaming\ 

通常本地应用数据文件夹解析为:

  C:\Documents and Settings\Bob\Application Data\ 

这个用户的特殊情况是什么奇怪的是,通常在HKEY_LOCAL_MACHINE中找到的几个注册表项实际上位于HKEY_CURRENT_USER中。他们在Windows 7上运行。



由于缺乏一个更好的词,有没有办法获得一个用户的真实应用程序数据,以便我可以浏览这种情况更好?如果是在CSIDL_APPDATA,CSIDL_COMMON_APPDATA和CSIDL_LOCAL_APPDATA特殊文件夹之间进行智能选择的问题,这样做的逻辑是什么?您可以告诉我,正在寻找一个可以根除用户正在运行的Windows版本或其特定PC配置的根目录下的正确的应用程序数据文件的多用途功能。


$ b $我发现这个Stack Overflow的帖子似乎有答案,但它正在使用.NET库中的函数,而且我使用的是Delphi 6.如果这个解决方案能够解决我的问题,有人能告诉我一个快速的方式来复制它在Delphi中:



如何获取当前用户的应用程序数据的路径,文件夹?

  //获取应用数据专用文件夹的功能。 
函数GetAppdataFolder:string;
begin
结果:= GetSpecialFolderLocation(CSIDL_APPDATA);
结束


解决方案

您链接到的.net代码使用 Environment.SpecialFolder.ApplicationData CSIDL_APPDATA 完全相同。所以你的代码已经等同于链接到的.net代码。这两者都与 FOLDERID_RoamingAppData



查看 FOLDERID_RoamingAppData 。它说:

 
默认路径%APPDATA%(%USERPROFILE%\AppData\Roaming)
传统默认路径%APPDATA %(%USERPROFILE%\Application Data)

默认路径是您将在Vista或更高版本中看到的。 Legacy Path是您在XP上看到的。



您观察到的不同行为只不过是XP和Vista / 7/8之间的预期区别。



在我的Windows机器上,

  Environment.GetFolderPath(Environment。 SpecialFolder.ApplicationData)

评估为

  C:\Users\heff\AppData\Roaming 



<换句话说,你的代码已经在做正确的事了。你根本不需要做任何修改。继续使用 GetSpecialFolderLocation(CSIDL_APPDATA)







这个用户的特殊情况是什么奇怪的是,通常在HKEY_LOCAL_MACHINE中找到的几个注册表项实际上位于HKEY_CURRENT_USER中。


这并不罕见通常应用程序在 HKLM 中配置默认​​设置,然后在应用程序首次运行时将其复制到 HKCU 。不知道有关设置的更多细节,很难对您的问题的那个方面发表评论。


I have a Delphi 6 application that, like most Windows applications, reads/writes data to the user's "local application data" folder. I use the code below to determine that folder. Up until now, that code worked for most of my users. I have encountered a user whose local application data is not in the expected folder:

C:\Users\Bob\AppData\Roaming\

Usually the local app data folder resolves to:

C:\Documents and Settings\Bob\Application Data\

What is odd about this user's particular situation is that several registry keys normally found in HKEY_LOCAL_MACHINE are actually located in HKEY_CURRENT_USER. They are running on Windows 7.

For lack of a better word, is there a way to get the "true" application data for a user so I can navigate this situation better? If it's a matter of intelligently choosing between the CSIDL_APPDATA, CSIDL_COMMON_APPDATA and CSIDL_LOCAL_APPDATA special folders, what is the logic for doing so? As you can tell I'm looking for an all-purpose function that can root out the correct application data folder regardless of the version of Windows the user is running or their specific PC configuration.

I found this Stack Overflow post that seems to have the answer but it is using function from the .NET library and I am using Delphi 6. If this solution answers my question, can someone tell me a quick way to replicate it in Delphi:

How can i get the path of the current user's "Application Data" folder?

// Function to get the app data special folder.
function GetAppdataFolder: string;
begin
   Result := GetSpecialFolderLocation(CSIDL_APPDATA);
end;

解决方案

The .net code you link to uses Environment.SpecialFolder.ApplicationData which is exactly the same as CSIDL_APPDATA. So your code is already equivalent to the .net code to which you link. And these both refer to the same location as FOLDERID_RoamingAppData.

Take a look at the documentation for FOLDERID_RoamingAppData. It says:

Default Path        %APPDATA% (%USERPROFILE%\AppData\Roaming)
Legacy Default Path %APPDATA% (%USERPROFILE%\Application Data) 

The "Default Path" is what you will see on Vista or later. The "Legacy Path" is what you see on XP.

The different behaviour that you have observed is nothing more than the expected difference between XP and Vista/7/8.

On my Windows machine,

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

evaluates to

C:\Users\heff\AppData\Roaming

In other words, your code is already doing the right thing. You do not need to make any changes to it at all. Carry on using GetSpecialFolderLocation(CSIDL_APPDATA).


What is odd about this user's particular situation is that several registry keys normally found in HKEY_LOCAL_MACHINE are actually located in HKEY_CURRENT_USER.

That's not uncommon. Quite often applications configure default settings in HKLM and then copy them to HKCU when the application is first run. Without knowing more details of the settings in question it's hard to comment on that aspect of your question.

这篇关于找到Windows用户的“真实”应用数据文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆