vb.net “访问文件路径被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22191225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:58:28  来源:igfitidea点击:

"Access to the file path is denied"

vb.net

提问by LOLZguy712

Whenever my application attempts to copy a file into C:\Users\???\AppData\Local, I get "Access to the file path is denied" Even when I'm running the app with admin it is still not working, can anybody help out?

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

回答by Bj?rn-Roger Kringsj?

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

Access to the path '...' is denied如果文件夹被标记为 ,您将收到错误消息ReadOnly。您可以尝试在复制文件之前删除此标志。

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
  • 应用程序数据:C:\Users{用户名}\AppData\Roaming
  • Cookie:C:\Users{用户名}\AppData\Roaming\Microsoft\Windows\Cookies
  • 历史记录:C:\Users{用户名}\AppData\Local\Microsoft\Windows\History
  • 本地设置:C:\Users{用户名}\AppData\Local
  • 我的文档:C:\Users{用户名}\Documents
  • NetHood:C:\Users{用户名}\AppData\Roaming\Microsoft\Windows\Network Shortcuts
  • PrintHood:C:\Users{用户名}\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
  • 最近:C:\Users{用户名}\AppData\Roaming\Microsoft\Windows\Recent
  • 发送到:C:\Users{用户名}\AppData\Roaming\Microsoft\Windows\SendTo
  • 开始菜单:C:\Users{用户名}\AppData\Roaming\Microsoft\Windows\Start Menu
  • 模板:C:\Users{用户名}\AppData\Roaming
  • Internet 临时文件:C:\Users{用户名}\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.
  • 它们设置了 FILE_ATTRIBUTE_REPARSE_POINT、FILE_ATTRIBUTE_HIDDEN 和 FILE_ATTRIBUTE_SYSTEM 文件属性。
  • 他们还将访问控制列表 (ACL) 设置为拒绝对所有人进行读取访问。

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