[英]C# streamwriter denies access to save xml file into MyDocuments
I created a little game with the option to save the character into an XML File, now I wanted the Savegame-Folder location to be at MyDocuments, but every time I try to save the XML I just get an access denied from my streamwriter. 我创建了一个小游戏,可以选择将角色保存到XML文件中,现在我希望Savegame-Folder位置位于MyDocuments中,但是每次尝试保存XML时,我都会受到Streamwriter的拒绝访问。 Does someone know how to fix that?
有人知道该如何解决吗?
Here's my code: 这是我的代码:
// 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"));
The 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);
}
}
}
I tried some of the solutions I found here on stackoverflow like this and this . 我尝试了一些在stackoverflow上找到的解决方案,例如this和this 。 But didn't work for me, maybe someone else has an idea how I can get access to that folder?
但是对我没有用,也许其他人对我如何访问该文件夹有一个想法? Or maybe just save it somewhere I actually have access, because ApplicationData and CommonApplicationData didn't work for me either.
也许只是将其保存在我实际可以访问的地方,因为ApplicationData和CommonApplicationData也不适合我。
Btw I'm using Virtual Box with Win10_Preview, I hope it's not because of this. 顺便说一句,我正在使用带有Win10_Preview的Virtual Box,我希望不是因为这个。
Edit: Before trying to save the files to MyDirectory I managed to save the files into my Debug folder of the project like this: 编辑:在尝试将文件保存到MyDirectory之前,我设法将文件保存到项目的Debug文件夹中,如下所示:
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Savegames\" + hero.created + ".xml";
gameSaver.SaveGameData(myCharacterObject, path);
Thanks to Jon Skeet I figured out that I was just using the directory name, instead of the full path to save my file. 多亏了Jon Skeet ,我才知道我只是在使用目录名,而不是保存文件的完整路径。 So I just fixed the code to this:
所以我将代码固定为:
// 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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.