直接在專案里面運行,是可以更改檔案夾權限的。
但是打包安裝后,發現只執行到 MessageBox.Show("44")
后面的 MessageBox.Show("66") 并不會彈出。
也就說, dir.SetAccessControl(dirSecurity)沒被執行。
求助,如何解決?打包的時候有什么注意事項,或者程式要怎樣修改?
代碼如下,C#,VB.net 都可以哈
Public Sub New()
InitializeComponent()
AddSecurityControllFolder()
End Sub
Private Shared Sub AddSecurityControllFolder()
Dim dir As DirectoryInfo = New DirectoryInfo(Application.StartupPath)
Dim dirSecurity As System.Security.AccessControl.DirectorySecurity = dir.GetAccessControl()
For Each rule As FileSystemAccessRule In dirSecurity.GetAccessRules(True, True, GetType(System.Security.Principal.NTAccount))
If rule.IdentityReference.Value.CompareTo("Everyone") = 0 Then
Return
End If
Next
AddSecurityControll2Folder(Application.StartupPath)
End Sub
Private Shared Sub AddSecurityControll2Folder(ByVal dirPath As String)
Dim dir As DirectoryInfo = New DirectoryInfo(dirPath)
MessageBox.Show("11")
Dim dirSecurity As System.Security.AccessControl.DirectorySecurity = dir.GetAccessControl()
MessageBox.Show("22")
Dim [inherits] As InheritanceFlags = InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit
Dim everyoneFileSystemAccessRule As FileSystemAccessRule = New FileSystemAccessRule("Everyone", FileSystemRights.FullControl, [inherits], PropagationFlags.None, AccessControlType.Allow)
Dim usersFileSystemAccessRule As FileSystemAccessRule = New FileSystemAccessRule("Users", FileSystemRights.FullControl, [inherits], PropagationFlags.None, AccessControlType.Allow)
MessageBox.Show("33")
Dim isModified As Boolean = False
dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, isModified)
dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, isModified)
MessageBox.Show("44")
dir.SetAccessControl(dirSecurity)
MessageBox.Show("66")
End Sub
uj5u.com熱心網友回復:
專案添加:app.manifest檔案,在檔案中查找:<requestedExecutionLevel level="asInvoker" uiAccess="false" />
改為
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
uj5u.com熱心網友回復:
這樣設定的話,就不每次啟動都是管理員運行了嗎?
這樣用戶體驗很差啊
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/264011.html
標籤:C#
