1
    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();
        }
    }

how do I make the above code stop throwing an InvalidOperationException?

The full error message is: Unable to generate a temporary class (result=1). error CS0016: Could not write to output file 'c:\Users[MYUSERNAME]\AppData\Local\Temp\czdgjjs0.dll' -- 'Access is denied.

I have no idea how to get around this error.

I am attempting to serialize my Level class which looks like this:

[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);
    }
}

My blocks class is implemented in a similar way and holds only a Position Vector that is saved.

Below, my save method:

    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);
    }

My program is an XNA game.

Runewake2
  • 471
  • 1
  • 7
  • 16
  • can you put how you call the `SerializeObject` ? – Turbot Mar 02 '12 at 03:51
  • as per Sheldon answer, if you are using ASP.NET make sure your IIS worker process have permission to access the tempDirectory otherwise pre-set to a dedicated folder. – Turbot Mar 02 '12 at 04:04

2 Answers2

1

Use COMODO antivirus and get CS0016 error?

Open COMODO Command Window (Main Window), and check the SANDBOX. If your application is listed as an application that has been flagged 'limited', simply right click and select the option from the popup to add your application as a Trusted Application. Or just uninstall COMODO and reboot.That should resolve the problem with CS0016 error.

Andrey Koleda
  • 141
  • 1
  • 3
0

The accepted answer here System.InvalidOperationException: Unable to generate a temporary class (result=1) is likely to have a reasonable solution for you.

One possibility they didn't suggest: if you're using ASP.NET is changing the temp directory in the web.config. Check the tempDirectory attribute of the compilation element (info here http://msdn.microsoft.com/en-us/library/s10awwz0.aspx ) and change to somewhere that your ASP.NET process does have access to.

Ultimately, though, your problem is that the process doing the serialization needs to generate and write some code to disk and doesn't have permissions. You can give that process permissions, change the location to somewhere it does have permissions, or use sgen.exe, depending on what works best for your situation.

Community
  • 1
  • 1
Sheldon Griffin
  • 4,405
  • 1
  • 14
  • 5
  • Upon further investigation the error has grown even more interesting. Every user (I'm the only one) have full access to my temp folder, so how do you/I get a permission denied? I have also attempted to run the binary as an administrator outside of VS, this had no effect. My program is an XNA game if that makes any difference. – Runewake2 Mar 02 '12 at 04:11