Xml序列化程序访问被拒绝

本文关键字:拒绝 访问 程序 序列化 Xml | 更新日期: 2023-09-27 18:26:28

    public void SerializeObject(string filename, T data)
    {
        // Get the path of the save game
        string fullpath = filename;
        // Open the file, creating it if necessary
        //if (container.FileExists(filename))
        //    container.DeleteFile(filename);
        FileStream stream = (FileStream)File.Open(fullpath, FileMode.OpenOrCreate);
        try
        {
            // Convert the object to XML data and put it in the stream
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(stream, data); //Thrown HERE
        }
        finally
        {
            // Close the file
            stream.Close();
        }
    }

如何使上面的代码停止抛出无效操作异常?

完整的错误消息是:无法生成临时类 (结果 = 1(。错误 CS0016:无法写入输出文件 'c:''Users[MYUSERNAME]''AppData''Local''Temp''czdgjjs0.dll' -- '访问被拒绝。

我不知道如何解决此错误。

我正在尝试序列化我的 Level 类,如下所示:

[Serializable]
public class Level : ISerializable
{
    public string Name { get; set; }
    public int BestTime { get; set; } //In seconds
    public List<Block> levelBlocks { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public Level()
    {
    }
    public Level(SerializationInfo info, StreamingContext ctxt)
    {
        this.Name = (String)info.GetValue("Name", typeof(String));
        this.BestTime = (int)info.GetValue("BestTime", typeof(int));
        this.levelBlocks = (List<Block>)info.GetValue("Blocks", typeof(List<Block>));
        this.Width = (int)info.GetValue("Width", typeof(int));
        this.Height = (int)info.GetValue("Height", typeof(int));
    }
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("Name", this.Name);
        info.AddValue("BestTime", this.BestTime);
        info.AddValue("Blocks", this.levelBlocks);
        info.AddValue("Width", this.Width);
        info.AddValue("Height", this.Height);
    }
}

我的块类以类似的方式实现,并且仅保存保存的位置向量。

下面是我的保存方法:

    public static void Save()
    {
        string filename = "saved.xml";
        Level toSave = new Level();
        toSave.levelBlocks = new List<Block>();
        //TODO: build toSave
        toSave.Name = "This is a level!";
        toSave.BestTime = 0;
        foreach (Entity e in EntityController.Entities)
        {
            if (e is Block)
            {
                toSave.levelBlocks.Add((Block)e);
                if (e.Position.X > toSave.Width)
                    toSave.Width = (int)e.Position.X;
                if (e.Position.Y > toSave.Height)
                    toSave.Height = (int)e.Position.Y;
            }
        }
        serializer.SerializeObject(filename, toSave);
    }

我的程序是一个XNA游戏。

Xml序列化程序访问被拒绝

使用COMODO防病毒软件并得到CS0016错误?

打开COMODO命令窗口(主窗口(,然后检查沙盒。如果您的应用程序被列为标记为"受限"的应用程序,只需右键单击并从弹出窗口中选择选项即可将您的应用程序添加为受信任的应用程序。或者只是卸载COMODO并重新启动。这应该可以解决CS0016错误的问题。

这里接受的答案 System.InvalidOperationException: 无法生成临时类 (result=1( 可能有一个合理的解决方案。

他们没有建议的一种可能性:如果您使用的是 ASP.NET 正在更改 web.config 中的临时目录。检查编译元素的 tempDirectory 属性(此处的信息 http://msdn.microsoft.com/en-us/library/s10awwz0.aspx (,并更改为您的 ASP.NET 进程有权访问的位置。

但归根结底,您的问题是执行序列化的进程需要生成一些代码并将其写入磁盘,并且没有权限。您可以授予该进程权限,将位置更改为它确实具有权限的位置,或使用 sgen.exe,具体取决于最适合您的情况的方法。