Search code examples
c#xmlwindowswinformsstreamwriter

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. 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. 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.

Btw I'm using Virtual Box with Win10_Preview, I hope it's not because of this.

Edit: Before trying to save the files to MyDirectory I managed to save the files into my Debug folder of the project like this:

path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Savegames\" + hero.created + ".xml";

gameSaver.SaveGameData(myCharacterObject, path);

Solution

  • Thanks to Jon Skeet I figured out that I was just using the directory name, instead of the full path to save my file. 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";