【问题标题】:XmlSerializer Access DeniedXmlSerializer 访问被拒绝
【发布时间】:2012-03-02 03:41:41
【问题描述】:
    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();
        }
    }

如何让上述代码停止抛出 InvalidOperationException?

完整的错误信息是: 无法生成临时类(结果=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 游戏。

【问题讨论】:

  • 你能说出你怎么称呼SerializeObject吗?
  • 根据 Sheldon 的回答,如果您使用的是 ASP.NET,请确保您的 IIS 工作进程有权访问 tempDirectory,否则会预先设置为专用文件夹。

标签: c# serialization


【解决方案1】:

使用 COMODO 杀毒软件出现 CS0016 错误?

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

【讨论】:

    【解决方案2】:

    这里接受的答案System.InvalidOperationException: Unable to generate a temporary class (result=1) 可能会为您提供合理的解决方案。

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

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

    【讨论】:

    • 经过进一步调查,该错误变得更加有趣。每个用户(我是唯一的)都可以完全访问我的临时文件夹,那么你/我如何获得拒绝的权限?我还尝试在 VS 之外以管理员身份运行二进制文件,但没有效果。如果这有什么不同的话,我的程序是一个 XNA 游戏。
    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 2021-03-06
    • 2011-05-15
    • 2010-09-06
    • 2014-03-22
    • 2013-08-10
    • 2018-04-20
    • 2012-08-23
    相关资源
    最近更新 更多