在具有游戏脚本功能的移动设备上创建文件时出现问题。拒绝访问路径

时间:2019-06-24 19:05:25

标签: c# unity3d

我制作了一个游戏,其中所有球员的数据都存储在XML文件中。在移动设备上制作游戏时,出现错误消息:

readonly string datapath = @"data.xml"; 
void CreateNewXMLFile(string name)
{
    XmlDocument gamedata = new XmlDocument();
    XmlNode docNode = gamedata.CreateXmlDeclaration("1.0", "UTF-8", null);
    gamedata.AppendChild(docNode);
    //Username
    XmlNode GameUsersNode = gamedata.CreateElement("GameUser");
    gamedata.AppendChild(GameUsersNode);
    //User
    XmlNode Username = gamedata.CreateElement("Username");
    Username.InnerText = name;
    GameUsersNode.AppendChild(Username);
    //Lastlogged
    XmlNode LastloggedTime = gamedata.CreateElement("LastloggedTime");
    LastloggedTime.InnerText = System.DateTime.Now.ToString();
    GameUsersNode.AppendChild(LastloggedTime);
    //Stage
    XmlNode Stage = gamedata.CreateElement("Stage");
    Stage.InnerText = "1";
    GameUsersNode.AppendChild(Stage);
    //Money
    XmlNode Money = gamedata.CreateElement("Money");
    Money.InnerText = "0";
    GameUsersNode.AppendChild(Money);
    //GreenHeroLvl
    XmlNode GreenHeroLvl = gamedata.CreateElement("GreenHeroLvl");
    GreenHeroLvl.InnerText = "0";
    GameUsersNode.AppendChild(GreenHeroLvl);
    //BlackHero
    XmlNode BlackHero = gamedata.CreateElement("BlackHero");
    BlackHero.InnerText = "0";
    GameUsersNode.AppendChild(BlackHero);
    //AssasinHero
    XmlNode AssasinHero = gamedata.CreateElement("AssasinHero");
    AssasinHero.InnerText = "0";
    GameUsersNode.AppendChild(AssasinHero);

    gamedata.Save(datapath);
}

错误是:

  

06-24 21:04:47.092:E / Unity(22918):UnauthorizedAccessException:拒绝访问路径“ /data.xml”。

1 个答案:

答案 0 :(得分:0)

您正在尝试写入系统路径/data.xml

在Android上,您通常无权向/根文件夹中写入任何内容!


我想你宁愿写例如到Application.persistentDataPath文件夹,如

readonly string datapath = Path.Combine(Application.persistentDataPath, "data.xml");

在Android上会导致有效路径,例如

/storage/emulated/0/Android/data/<packagename>/files/data.xml