vb.net Visual Basic .NET 对路径“C:\”的访问被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18390625/
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
Visual Basic .NET Access to the path 'C:\' is denied
提问by Bob Ma
I am using Visual Studio 2012 and Visual Basic .NET to build the application (with Windows 7)
我正在使用 Visual Studio 2012 和 Visual Basic .NET 来构建应用程序(使用 Windows 7)
I currently build the install package and i install to another pc to test program.
我目前构建安装包,我安装到另一台电脑来测试程序。
When I was creating txt file by my app, i was getting "Access to the path 'C:\' is denied" error
当我通过我的应用程序创建 txt 文件时,我收到“访问路径 'C:\' 被拒绝”错误
I know that I was using sub account to run my application.
我知道我正在使用子帐户来运行我的应用程序。
If I run as administrator, it is was working fine. However, the program should be able to run application without using administrator account .
如果我以管理员身份运行,它工作正常。但是,该程序应该能够在不使用管理员帐户的情况下运行应用程序。
These are what I tried to solve this problem so far.
到目前为止,这些是我试图解决这个问题的方法。
First, I change the location to write file in "Program file folder" such as "C:\Program file\My App"
首先,我在“程序文件夹”中更改写入文件的位置,例如“C:\Program file\My App”
However, it didn't work
然而,它没有用
Second, I was trying to change
其次,我试图改变
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
to
到
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
at app.config
在 app.config
However, After I replaced to "requireAdministrator" , I am getting compiling error
但是,在我替换为 "requireAdministrator" 后,出现编译错误
ClickOnce does not support the request execution level 'requireAdministrator'. WindowsApplication2
Third, I went to Computer->manager->service-> and Enable Application experience.
第三,我去计算机->管理器->服务->启用应用程序体验。
However, it does not work.
但是,它不起作用。
I am not really sure what to do for this problem.
我真的不知道该怎么办这个问题。
Does anybody know any solution ?
有人知道任何解决方案吗?
thanks
谢谢
回答by Private
Dim UserAccount As String = "<user here>" 'Specify the user here
Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo("path")
Dim FolderAcl As New DirectorySecurity
FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
FolderAcl.SetAccessRuleProtection(False, False)
FolderInfo.SetAccessControl(FolderAcl)
Note you have to add: Imports.System.Security.AccessControl
注意你必须添加:Imports.System.Security.AccessControl
By the way I got it from here: Give folder full access when createdso credit goes to that person. (Jacques Bronkhorst) Hope this solves the issue :)
顺便说一下,我是从这里得到的:创建文件夹时授予完全访问权限,以便该人获得功劳。(Jacques Bronkhorst) 希望这能解决问题:)

