1

During my game, you can save a map and store the tiles in the map into a xml file.
So far no problem. The problem starts when I try to save it it gives me a UnauthorizedAccessException error somehow.

The folder is located in the installation dir of my game:

instal_dir/data/maps/

I checked to make sure, but the folder is created succesfully with the correct permissions (write, read and execute).

Am I doing something wrong?

Here is my code:

private void CreateXMLOfMap()
{
    List<Tile> tiles = mapContainer.GetTileList();
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
    doc.AppendChild(docNode);

    XmlNode rootNode = doc.CreateElement("Map");
    doc.AppendChild(rootNode);

    XmlNode mapName = doc.CreateElement("Name");
    mapName.AppendChild(doc.CreateTextNode("custom_map"));
    rootNode.AppendChild(mapName);

    XmlNode tilesNode = doc.CreateElement("Tiles");
    rootNode.AppendChild(tilesNode);

    for (int i = 0; i < tiles.Count; i++ )
    {
        XmlNode tileNode = doc.CreateElement("Tile");
        tilesNode.AppendChild(tileNode);

        XmlNode positionNode = doc.CreateElement("Position");
        tileNode.AppendChild(positionNode);
        XmlNode xNode = doc.CreateElement("X");
        xNode.AppendChild(doc.CreateTextNode(tiles[i].GetTilePosition().X.ToString()));
        positionNode.AppendChild(xNode);
        XmlNode yNode = doc.CreateElement("Y");
        yNode.AppendChild(doc.CreateTextNode(tiles[i].GetTilePosition().Y.ToString()));
        positionNode.AppendChild(yNode);

        XmlNode textureNode = doc.CreateElement("Texture");
        textureNode.AppendChild(doc.CreateTextNode(tiles[i].GetTileInfo().Name.ToString()));
        tileNode.AppendChild(textureNode);

        XmlNode YFrameNode = doc.CreateElement("YFrame");
        YFrameNode.AppendChild(doc.CreateTextNode(tiles[i].GetCurrentFrame().Y.ToString()));
        tileNode.AppendChild(YFrameNode);
    }

    doc.Save(Constants.MAPS_DIRECTORY);
}
Omar
  • 16,329
  • 10
  • 48
  • 66
DijkeMark
  • 1,284
  • 5
  • 19
  • 41
  • 2
    What if you will run your game with administrator rights? – acrilige Jan 16 '13 at 14:22
  • Have you tried running it as the administrator? What is the **full** path to your installation directory of your game? Is it in "Program Files"? If it is, I would suggest not writing the .xml file to your installation directory but to your "My Documents" folder or something like that. – antonijn Jan 16 '13 at 14:24
  • 1
    This a good practice to separate binaries from user data. Most of time the binaries go to `c:\Program files`, while the user data go under user's `%APPDATA%`. This patterns allows to have "protected" binaries from unfortunate or malicious update, while letting each user have its own private data. – Steve B Jan 16 '13 at 14:32

3 Answers3

2

I think the problem is that you don't run the application as administrator and this cause an UnauthorizedAccessException. So you should force the system to run it as administrator. Try to change the Application Manifest File from:

<requestedExecutionLevel level="asInvoker" uiAccess="false"/>

to:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>

EDIT:

How can I run the game with admin rights from visual studio?

Sometimes I get this excpetion when I debug an application with vs2010 even if I'm the system administrator. However if you want to debug the application as administrator try to right click visual studio icon and then Run as Administrator.

Community
  • 1
  • 1
Omar
  • 16,329
  • 10
  • 48
  • 66
1

Okay, so I found the problem that caused this exception to show up. It appeard that I only included the directory, but not the file name. So it tried to store the content into the directory, instead of into a xml file.

All I had to change was to string that goes into

doc.Save(PATH_TO_DIR_WITH_FILE_NAME);

But I'll keep in my, if I continue like this, I might end up needing administrator righs, so I'll need to change it sooner or later.

Thanks all for your advice and help!

DijkeMark
  • 1,284
  • 5
  • 19
  • 41
  • Thanks for this update! I tried with admin rights but it never worked. Also, using admin right for a simple file write seems too much. – john Aug 20 '14 at 06:27
1

I have similar problem, but I think, that calling Administrator rights for simple file operations is incorrect. So I manually delete exist file before saving:

File.Delete(mainXmlPath);
xmlDocument.Save(mainXmlPath);
Andrey
  • 63
  • 1
  • 6