none
File access denied in my own Documents folder RRS feed

  • Question

  • It seems that recently I cannot write to file programatically.

    I made sure that the C:\Users\MeMyselfI\Documents\MyFolder is truly writable, that the file exists

    I made sure the app.manifest exists

    Now this simple code triggers the access denied  exception: 'System.UnauthorizedAccessException' in mscorlib.dll

                Dim filePath As String
                filePath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "MyFolder\Test.txt")
    
               
                IO.File.WriteAllText(filePath, "some text")
            Catch ex As Exception
                Debug.Print("Error : " & ex.Message)
            End Try

    Tried running VS2015 as Administrator, tried running VS2017 as administrator...Windows 10, nothing gives.

    Any hint to what is happening there?

    Tuesday, October 2, 2018 11:59 AM

Answers

  • Alright, it was a Defender feature! the Controlled folder access feature in Defender.

    I disabled the Controlled Folder Access and it went back to "normal".

    Now I need to find out why that disabled my writing and how to have the protection and still being able to write to my own Documents folder...

    The answer was given to me in the windows Community forum.

    Cheers


    Wednesday, October 3, 2018 6:07 PM

All replies

  • What is the exact error message?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 2, 2018 12:44 PM
    Moderator
  • The exception is UnauthorizedAccessException:

    Exception levée : 'System.UnauthorizedAccessException' dans mscorlib.dll
    Exception levée : 'System.UnauthorizedAccessException' dans mscorlib.dll
    Error message: L'accès au chemin d'accès est refusé. mscorlib

    Tuesday, October 2, 2018 12:49 PM
  • Ok, I have some trouble saving from some applications to the C:\Users\MyUser\Documents.

    It's probably a Windows issue.

    Tuesday, October 2, 2018 1:47 PM
  • It seems that recently I cannot write to file programatically.

    I made sure that the C:\Users\MeMyselfI\Documents\MyFolder is truly writable, that the file exists

    I made sure the app.manifest exists

    Now this simple code triggers the access denied  exception: 'System.UnauthorizedAccessException' in mscorlib.dll

                Dim filePath As String
                filePath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "MyFolder\Test.txt")
    
               
                IO.File.WriteAllText(filePath, "some text")
            Catch ex As Exception
                Debug.Print("Error : " & ex.Message)
            End Try

    Tried running VS2015 as Administrator, tried running VS2017 as administrator...Windows 10, nothing gives.

    Any hint to what is happening there?

    Hi

    I reproduced the filepath you used, and tried your code. I had no issues at all, NotePad opened with the 'new' contents in the file.

        Dim filePath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "MyFolder\Test.txt")
        IO.File.WriteAllText(filePath, "some text")
        Process.Start(filePath)


    Regards Les, Livingston, Scotland

    Tuesday, October 2, 2018 1:49 PM
  • Thanks,

    I'll revert the last windows update from September and start from there.

    Tuesday, October 2, 2018 1:55 PM
  • See my post on this 

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/ad99e70d-9383-4840-aaa7-738cd38f0c1a/unauthorizedaccessexception-attempting-to-get-documents-folder?forum=vbgeneral


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 2, 2018 2:00 PM
    Moderator
  • Alright, it was a Defender feature! the Controlled folder access feature in Defender.

    I disabled the Controlled Folder Access and it went back to "normal".

    Now I need to find out why that disabled my writing and how to have the protection and still being able to write to my own Documents folder...

    The answer was given to me in the windows Community forum.

    Cheers


    Wednesday, October 3, 2018 6:07 PM