C# streamwriter 拒绝访问将 xml 文件保存到 MyDocuments
C# streamwriter denies access to save xml file into MyDocuments
我创建了一个小游戏,可以选择将角色保存到 XML 文件中,现在我希望 Savegame-Folder 位置位于 MyDocuments,但每次我尝试保存 XML 我的流媒体作者刚刚拒绝访问。有人知道如何解决吗?
这是我的代码:
// Create the folder into MyDocuments (works perfectly!)
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"Arena\Savegames\"));
// This one should the save the file into the directory, but it doesn't work :/
path = Path.GetDirectoryName(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Arena\Savegames\" + hero.created + ".xml"));
Streamwriter:
public class SaveLoadGame
{
public void SaveGameData(object IClass, string filename)
{
StreamWriter saveGameWriter = null;
try
{
XmlSerializer saveGameSerializer = new XmlSerializer((IClass.GetType()));
saveGameWriter = new StreamWriter(filename);
saveGameSerializer.Serialize(saveGameWriter, IClass);
}
finally
{
if (saveGameWriter != null)
saveGameWriter.Close();
saveGameWriter = null;
}
}
}
public class LoadGameData<T>
{
public static Type type;
public LoadGameData()
{
type = typeof(T);
}
public T LoadData(string filename)
{
T result;
XmlSerializer loadGameSerializer = new XmlSerializer(type);
FileStream dataFilestream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
result = (T)loadGameSerializer.Deserialize(dataFilestream);
dataFilestream.Close();
return result;
}
catch
{
dataFilestream.Close();
return default(T);
}
}
}
我尝试了一些在 Whosebug 上找到的解决方案,例如 this and this。但是对我不起作用,也许其他人知道我如何访问该文件夹?或者也许只是将它保存在我实际可以访问的地方,因为 ApplicationData 和 CommonApplicationData 对我也不起作用。
顺便说一下,我在 Win10_Preview 中使用 Virtual Box,我希望不是因为这个。
编辑: 在尝试将文件保存到 MyDirectory 之前,我设法将文件保存到项目的 Debug 文件夹中,如下所示:
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Savegames\" + hero.created + ".xml";
gameSaver.SaveGameData(myCharacterObject, path);
感谢 Jon Skeet 我发现我只是使用目录名,而不是完整路径来保存我的文件。所以我只是将代码固定为:
// Creating the folder in MyDocuments
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"Arena\Savegames\"));
// Setting the full path for my streamwriter
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Arena\Savegames\" + hero.created + ".xml";
我创建了一个小游戏,可以选择将角色保存到 XML 文件中,现在我希望 Savegame-Folder 位置位于 MyDocuments,但每次我尝试保存 XML 我的流媒体作者刚刚拒绝访问。有人知道如何解决吗?
这是我的代码:
// Create the folder into MyDocuments (works perfectly!)
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"Arena\Savegames\"));
// This one should the save the file into the directory, but it doesn't work :/
path = Path.GetDirectoryName(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Arena\Savegames\" + hero.created + ".xml"));
Streamwriter:
public class SaveLoadGame
{
public void SaveGameData(object IClass, string filename)
{
StreamWriter saveGameWriter = null;
try
{
XmlSerializer saveGameSerializer = new XmlSerializer((IClass.GetType()));
saveGameWriter = new StreamWriter(filename);
saveGameSerializer.Serialize(saveGameWriter, IClass);
}
finally
{
if (saveGameWriter != null)
saveGameWriter.Close();
saveGameWriter = null;
}
}
}
public class LoadGameData<T>
{
public static Type type;
public LoadGameData()
{
type = typeof(T);
}
public T LoadData(string filename)
{
T result;
XmlSerializer loadGameSerializer = new XmlSerializer(type);
FileStream dataFilestream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
result = (T)loadGameSerializer.Deserialize(dataFilestream);
dataFilestream.Close();
return result;
}
catch
{
dataFilestream.Close();
return default(T);
}
}
}
我尝试了一些在 Whosebug 上找到的解决方案,例如 this and this。但是对我不起作用,也许其他人知道我如何访问该文件夹?或者也许只是将它保存在我实际可以访问的地方,因为 ApplicationData 和 CommonApplicationData 对我也不起作用。
顺便说一下,我在 Win10_Preview 中使用 Virtual Box,我希望不是因为这个。
编辑: 在尝试将文件保存到 MyDirectory 之前,我设法将文件保存到项目的 Debug 文件夹中,如下所示:
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Savegames\" + hero.created + ".xml";
gameSaver.SaveGameData(myCharacterObject, path);
感谢 Jon Skeet 我发现我只是使用目录名,而不是完整路径来保存我的文件。所以我只是将代码固定为:
// Creating the folder in MyDocuments
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"Arena\Savegames\"));
// Setting the full path for my streamwriter
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Arena\Savegames\" + hero.created + ".xml";