簡體   English   中英

XmlSerializer訪問被拒絕

[英]XmlSerializer Access Denied

    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游戲。

使用COMODO防病毒軟件並獲得CS0016錯誤?

打開COMODO命令窗口(主窗口),然后檢查SANDBOX。 如果您的應用程序被列為已標記為“受限”的應用程序,則只需右鍵單擊並從彈出菜單中選擇選項,即可將您的應用程序添加為“受信任的應用程序”。 或者只是卸載COMODO並重新啟動。這應該可以解決CS0016錯誤的問題。

在此處可以接受的答案System.InvalidOperationException:無法生成臨時類(結果= 1)可能為您提供了一個合理的解決方案。

他們沒有建議的一種可能性:如果您使用的是ASP.NET,則正在更改web.config中的temp目錄。 檢查編譯元素的tempDirectory屬性(信息在此處http://msdn.microsoft.com/zh-cn/library/s10awwz0.aspx ),然后更改為ASP.NET進程確實可以訪問的位置。

但是最終,您的問題是,進行序列化的過程需要生成一些代碼並將其寫入磁盤,並且沒有權限。 您可以授予該進程權限,將位置更改為它確實具有權限的位置,或者使用sgen.exe,這取決於最適合您的情況的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM