0

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);
gorkem
  • 731
  • 1
  • 10
  • 17
NakedPython
  • 920
  • 2
  • 7
  • 27
  • 1
    Um, you're calling `GetDirectoryName`, so that's going to return a directory name. You can't write to a directory name. Also, why are you calling `Path.Combine` but only passing a single value? That looks very broken to me... – Jon Skeet May 23 '17 at 08:14
  • Sorry if looks broken, just started 1 Week ago with C# and still have some problems with it :s I should have mentioned that before I tried to save my files into MyDocuments, I actually managed to save them into the Debug Folder of my Project, with this code: // Moved code to the question // So I thought I could just reuse it again and just change the location to MyDocuments. Also my SaveGameData Method needs an object and a string path, so wouldn't I need the Directory name here? – NakedPython May 23 '17 at 08:26
  • 1
    Please don't put code in comments - edit your question instead. Fundamentally it sounds like you should learn to use the debugger at this point - look at the filename you're trying to save to, and I suspect you'll see what I mean. (You need the full path to the file you want to write to - you're *just* using the directory name...) – Jon Skeet May 23 '17 at 08:28
  • Well I debugged a bit and saw the problem, I managed to get the full path and not only the directory name, but my streamwriter now tries to save my file into "myprojectfolder/debug/MyDocuments/Arena/Savegamesfolder/file.xml", even tho the parameter he gets is just the "MyDocuments/Arena/Savegamefolder/file.xml" - I update my code in the question, something I forgot to do or did wrong? – NakedPython May 23 '17 at 09:09
  • Well "MyDocuments" is a relative directory name... and you're calling `Environment.SpecialFolder.MyDocuments.ToString()` which is just going to be "MyDocuments". You need to call `GetFolderPath`... – Jon Skeet May 23 '17 at 09:22

1 Answers1

0

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";
NakedPython
  • 920
  • 2
  • 7
  • 27