Search code examples
c#serializationbinarybinary-serializationunauthorizedaccessexcepti

Serialization problem : System.UnauthorizedAccessException


I am getting this error: An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

Additional information: Access to the path 'C:\Users\Storm Kiernan\Desktop(NEW)Archetype Development Kit\Laboratory\Laboratory\bin\x86\Debug\lol.dataf' is denied.

From trying to serialize any object via this code:

    public static void BinarySerialize<T>(this T t, string path)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(path);
        directoryInfo.EnsureDirectory();

        using (FileStream stream = new FileStream(directoryInfo.FullName, FileMode.OpenOrCreate))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, t);
        }
    }

This project is located on my desktop, there is another in the VS/Projects in my Documents folder that has code almost identical to this and it runs just fine. The only difference being its not generic. Any ideas?


Solution

  • OK so the problem I was having was that I would basically create a folder called "lol.dataf" and try to write to a folder and not a file. Hence the fact that it could not be written to. The compiler should have printed : I.D.10-T but alas, it did not. Thank you for all your help guys.