none
Unauthorized Exception when using File.Replace with no permission to create folders RRS feed

  • Question

  • Hello All,

    I get an unexpected error when using File.Replace. The error occurs when the user has no permission to create folders.

    Sample code:

    static void Main(string[] args)
    {
        string sPath = @"C:\Temp\TestRechten\MapAlleenSchrijfRechten";
        string sFile = Path.Combine(sPath, "Test.xml");
        string sTemp = Path.Combine(sPath, "Test.xml.tmp");
        string sBackup = Path.Combine(sPath, "Test.xml.bu");
    
        XmlDocument xdTest = new XmlDocument();
        XmlNode xnNode = xdTest.CreateElement("Test");
        xdTest.AppendChild(xnNode);
    
        if (File.Exists(sTemp))
            File.Delete(sTemp);
    
        using (StreamWriter swSave = new StreamWriter(File.Open(sTemp, FileMode.CreateNew), Encoding.UTF8))
            xdTest.Save(swSave);
    
        if (File.Exists(sFile))
        {
            //Works
            //if (File.Exists(sBackup) == true)
            //    File.Delete(sBackup);
            //File.Move(sFile, sBackup);
            //File.Move(sTemp, sFile);
    
            //UnauthorizedAccessException
            File.Replace(sTemp, sFile, sBackup);
        }
        else
            File.Move(sTemp, sFile);
    }

    The permissions on the folder are set as followed:

    

    The settings are in Dutch but I think the order is the same on any version. The code does not throw an error when the right to create folders is granted (The one with red circle in the picture above).

    The documentation on the function only specifies the FileIOPermissionAccess.Write is needed for the backup file.

    Any suggestions?

    Ton

    Monday, September 11, 2017 8:53 AM

All replies

  • Hi Ton Sleegers,

    Thank you for posting here.

    >>The documentation on the function only specifies the FileIOPermissionAccess.Write is needed for the backup file.

    If we want to use File.Replace Method, we should pay attention to these four ways to avoid the Unauthorized exception.

    1. The sourceFileName or destinationFileName parameter of File.Replace Method specifies a file that is not read-only.
    2. This operation is supported on the current platform.
    3. Source or destination parameters specify a File instead of a directory.
    4. The caller have the required permission.

    For the first requirement, you have two ways to resolve. First, like what you said is to set the FileIOPermissionAccess. It is a good way. Second is to set the file permission on File properties.

    >>Unauthorized Exception when using File.Replace with no permission to create folders

    However, according to your description of your title, it is more related to the third way "Source or destination parameters specify a File instead of a directory.". If you use directory of source or destination parameters of File.Replace Method, it will throw Unauthorized exception.

    And for the picture of permission, where do you change the permission?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, September 12, 2017 8:18 AM
    Moderator