在Windows上保存依赖于机器(而不是用户)的ini文件的位置 [英] Where to save ini file dependent to machine (not user) on windows

查看:143
本文介绍了在Windows上保存依赖于机器(而不是用户)的ini文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在当前用户的配置文件( C:\Documents and Settings\< CurrentUser> \Application Data\MyApplication\MySettings.ini)中的INI文件中存储设置在WinXP下)。但是我已经意识到,这些设置中的一些设备对于机器而言并不是用户所特有,因此希望(实际上需要)将它们保存在所有用户的单个位置。

My application is currently storing settings in an INI file under the current user's profile ( C:\Documents and Settings\<CurrentUser>\Application Data\MyApplication\MySettings.ini under WinXP). But I've realised some of these settings are unique to the machine not the user and thus want (actually need) to save them in a single location for all users.

在Windows XP(和更高版本)上是否有文件夹位置,可以存储用户独立设置?

注意:我不想将它们存储在与我的应用程序相同的文件夹中,也不想将它们存储在注册表中。

NOTE: I don't want to store them in the same folder as my application nor do I want to store them in the registry.

我注意到C:\Documents and Settings\下有一个所有用户文件夹?我是否应该存储在那里?

I notice there is an "All Users" folder under "C:\Documents and Settings\"? Should I be storing under there?

奖金积分:我更有可能将答案授予谁也可以告诉我如何返回这个路径从Windows在Delphi 7中。

Bonus Points: I'm more likely to award the answer to whoever can also tell me how to return this path from Windows in Delphi 7.

推荐答案

对于XP,Windows提供 SHGetFolderPath()获取已知位置。您要查找的CSIDL是 CSIDL_COMMON_APPDATA ,描述为:

For XP, Windows provides SHGetFolderPath() to get a known location. The CSIDL that you're looking for is CSIDL_COMMON_APPDATA, described as:


包含所有用户的应用程序数据的文件系统目录。一个典型的路径是C:\Documents and Settings\All Users\Application Data。该文件夹用于不是用户特定的应用程序数据。例如,应用程序可以在 CSIDL_COMMON_APPDATA 文件夹中存储拼写检查字典,剪贴画数据库或日志文件。这个信息不会漫游,任何使用电脑的人都可以使用。

The file system directory that contains application data for all users. A typical path is "C:\Documents and Settings\All Users\Application Data". This folder is used for application data that is not user specific. For example, an application can store a spell-check dictionary, a database of clip art, or a log file in the CSIDL_COMMON_APPDATA folder. This information will not roam and is available to anyone using the computer.

对于Vista及更高版本,这已被=http://msdn.microsoft.com/en-us/library/bb762188%28VS.85%29.aspx =nofollow noreferrer> SHGetKnownFolderPath()虽然SHGetFolderPath()仍然可用一个包装功能。如果您使用真正的Vista通话,您应该使用 FOLDERID_ProgramData 而不是 CSIDL_COMMON_APPDATA

For Vista and later, this has been replaced with SHGetKnownFolderPath() although SHGetFolderPath() is still available as a wrapper function for that. If you use the real Vista call, you should use FOLDERID_ProgramData instead of CSIDL_COMMON_APPDATA.

此链接这里似乎显示出一种方式。

This link here seems to show a way of doing it.

似乎归结为这一点(谨慎对待这一点,我不知道Delphi那么好):

It seems to boil down to this (treat this with circumspection, I don't know Delphi that well):

function ShGetKnownFolderPath (
    const rfid:   TGUID;
    dwFlags:      DWord;
    hToken:       THandle;
    out ppszPath: PWideChar): HResult;
var
    Shell: HModule;
    Fn: TShGetKnownFolderPath;
begin
    Shell := LoadLibrary ('shell32.dll');
    Win32Check(Shell <> 0);
    try
        @Fn := GetProcAddress (Shell, 'SHGetKnownFolderPath');
        Win32Check (Assigned (Fn));
        Result := Fn (rfid, dwFlags, hToken, ppszPath);
    finally
        FreeLibrary (Shell);
    end;
end;

 

function GetKnownFolderPath (
    const rfid: TGUID;
    dwFlags:    DWord;
    hToken:     THandle): WideString;
var
    buffer: PWideChar;
    ret: HResult;
begin
    ret :=ShGetKnownFolderPath (rfid, dwFlags, hToken, buffer);
    OleCheck (ret);
    try
        Result := buffer;
    finally
        CoTaskMemFree (buffer);
    end;
end;

此页面提供了所有 CSIDL _ * FOLDERID _ * 值。请记住,您应该为用户特定的数据使用这些功能,而不是硬编码的值,如C:\Documents and Settings\< CurrentUser> \Application Data\ 。可能是Windows的不同语言版本使用不同的目录名,或者用户可以随意移动其数据区域。

This page provides a list of all the CSIDL_* and FOLDERID_* values. Keep in mind you should be using these functions for your user-specific data as well, not hard-coded values like "C:\Documents and Settings\<CurrentUser>\Application Data\". It may be that different language versions of Windows use different directory names or it's possible that users can freely move their data areas around.

这篇关于在Windows上保存依赖于机器(而不是用户)的ini文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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