简体   繁体   中英

“Access to the file path is denied”

每当我的应用程序尝试将文件复制到C:\\ Users \\ ??? \\ AppData \\ Local时,我得到“拒绝访问文件路径”即使我使用管理员运行应用程序它仍然无法正常工作,有人帮忙吗?

You'll get a Access to the path '...' is denied error if the folder is flagged ReadOnly . You could try to remove this flag prior to copying the file.

Dim info As DirectoryInfo = New DirectoryInfo("C:\Users\???\AppData\Local")

If (info.Exists AndAlso ((info.Attributes And FileAttributes.[ReadOnly]) = FileAttributes.[ReadOnly])) Then
    info.Attributes = (info.Attributes Xor FileAttributes.[ReadOnly])
End If

Update

I did a google search and found these link which seems interesting:

"Those are junction points : hidden, protected operating system files that are not meant to be accessed by users. Each one points to a user-accessible folder:"

  • Application Data: C:\\Users{user name}\\AppData\\Roaming
  • Cookies: C:\\Users{user name}\\AppData\\Roaming\\Microsoft\\Windows\\Cookies
  • History: C:\\Users{user name}\\AppData\\Local\\Microsoft\\Windows\\History
  • Local Settings: C:\\Users{user name}\\AppData\\Local
  • My Documents: C:\\Users{user name}\\Documents
  • NetHood: C:\\Users{user name}\\AppData\\Roaming\\Microsoft\\Windows\\Network Shortcuts
  • PrintHood: C:\\Users{user name}\\AppData\\Roaming\\Microsoft\\Windows\\Printer Shortcuts
  • Recent: C:\\Users{user name}\\AppData\\Roaming\\Microsoft\\Windows\\Recent
  • SendTo: C:\\Users{user name}\\AppData\\Roaming\\Microsoft\\Windows\\SendTo
  • Start Menu: C:\\Users{user name}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu
  • Templates: C:\\Users{user name}\\AppData\\Roaming
  • Temporary Internet Files: C:\\Users{user name}\\AppData\\Local\\Microsoft\\Windows\\Temporary Internet Files

"These junction points can be identified as follows:"

  • They have the FILE_ATTRIBUTE_REPARSE_POINT, FILE_ATTRIBUTE_HIDDEN, and FILE_ATTRIBUTE_SYSTEM file attributes set.
  • They also have their access control lists (ACLs) set to deny read access to everyone.

So...

I don't know if this is recommended, will work or throw an error, but you could try to remove these flags:

info.Attributes = (attributes Xor (FileAttributes.ReparsePoint Or FileAttributes.Hidden Or FileAttributes.System))

And add then back afterwards:

info.Attributes = (attributes Or (FileAttributes.ReparsePoint Or FileAttributes.Hidden Or FileAttributes.System))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM