English 中文(简体)
你是如何用程序找到Vista的“保存的游戏”文件夹的?
原标题:
  • 时间:2009-04-09 02:24:16
  •  标签:

我正在使用XNA,我想将文件保存到Vista的“保存的游戏”文件夹中。

我可以使用Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)获得类似的特殊文件夹,如My Documents,但我找不到与Saved Games文件夹等效的文件夹。如何访问此文件夹?

最佳回答

http://msdn.microsoft.com/en-us/library/bb200105.aspx#ID2EWD

看起来您需要使用Microsoft.Xna.Framework.Storage和StorageLocation类来完成您需要的操作。

Currently, the title location on a PC is the folder where the executable resides when it is run. Use the TitleLocation property to access the path.

User storage is in the My Documents folder of the user who is currently logged in, in the SavedGames folder. A subfolder is created for each game according to the titleName passed to the OpenContainer method. When no PlayerIndex is specified, content is saved in the AllPlayers folder. When a PlayerIndex is specified, the content is saved in the Player1, Player2, Player3, or Player4 folder, depending on which PlayerIndex was passed to BeginShowStorageDeviceSelector.

问题回答

它没有特殊的文件夹常量,所以只使用系统变量。根据维基百科的这篇文章特殊文件夹,保存的游戏文件夹只是:

保存的游戏%USERPROFILE%保存的游戏Vista

因此,代码将是:

 string sgPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "saved games"));

...

编辑:如果根据评论,本地化是一个问题,并且根据您的问题,您仍然希望直接访问Saved Games文件夹,而不是使用API,那么以下可能会有所帮助。

使用RedGate反射器,我们可以看到GetFolderPath的实现如下:

public static string GetFolderPath(SpecialFolder folder)
{
    if (!Enum.IsDefined(typeof(SpecialFolder), folder))
    {
        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GetResourceString("Arg_EnumIllegalVal"), new object[] { (int) folder }));
    }
    StringBuilder lpszPath = new StringBuilder(260);
    Win32Native.SHGetFolderPath(IntPtr.Zero, (int) folder, IntPtr.Zero, 0, lpszPath);
    string path = lpszPath.ToString();
    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
    return path;
}

所以,也许你认为我所需要的就是创建我自己版本的这个方法,并将保存的游戏的文件夹id传递给它。那行不通。Vista之前的那些文件夹ID实际上是CSIDL。可以找到它们的列表此处。注意注意:

在发布Vista时,微软用KNOWNFOLDERID取代了CLSIDL。可以找到KNOWNFOLDERID列表此处。保存的游戏KNOWNFOLDERID为FOLDRID_SavedGames。

但是,您不只是将新的const传递给旧的、基于CLSIDL的、SHGetFolderPath Win32函数。根据本文,已知文件夹,正如您所料,有一个名为SHGetKnownFolderPath,您将新的FOLDERID_SavedGames常量传递到该路径,该路径将以本地化形式返回Saved Games文件夹的路径。

我发现获取Saved Games路径的最简单方法是读取注册表值,如下所示:

var defaultPath = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Saved Games");
var regKey = "HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerShell Folders";
var regKeyValue = "{4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4}";
var regValue = (string) Registry.GetValue(regKey, regKeyValue, defaultPath);

我多次通过Shell更改我保存的游戏的位置,每次都会更改此密钥的值。我使用USERPROFILE/Saved Games作为默认设置,因为我认为这适用于有人从未更改位置的默认情况。





相关问题
热门标签