I am tryting to change permission for a specific path under root "C:\BackgammonTxt\AppDataFile.txt"
With CreateNew sub I first create the folder BackgammonTxt and AppDataFile.txt to write data to it. CreateNew Sub takes part through installer class from ovverride install sub.
But I am getting a message 36 times An unauthorized operation was attempted with ok button. I am trying to Clear all permissions and give full control on any user group for the specific path I
mentioned above which is "C:\BackgammonTxt\AppDataFile.txt"
Firstly I am trying for root c:\ secondly for the folder BackgammonTxt and lastly for the file...
Where I am wrong? It's the first time I am dealing with something like that as a junior developer, so any help will be much appreciated. Thank you so much in advanced.
With CreateNew sub I first create the folder BackgammonTxt and AppDataFile.txt to write data to it. CreateNew Sub takes part through installer class from ovverride install sub.
But I am getting a message 36 times An unauthorized operation was attempted with ok button. I am trying to Clear all permissions and give full control on any user group for the specific path I
mentioned above which is "C:\BackgammonTxt\AppDataFile.txt"
Firstly I am trying for root c:\ secondly for the folder BackgammonTxt and lastly for the file...
Where I am wrong? It's the first time I am dealing with something like that as a junior developer, so any help will be much appreciated. Thank you so much in advanced.
Code:
Public Sub ChangeAllPermissions(ByVal path As String)
Dim FolderPath As String = path 'Specify the folder here
Dim UserAccount As String '= GetUserName() 'Specify the user here
Dim MyDomain As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
UserAccount = MyDomain
Dim fInfo As New IO.DirectoryInfo(FolderPath)
Dim FolderAcl As DirectorySecurity = fInfo.GetAccessControl()
FolderAcl.PurgeAccessRules(New Principal.NTAccount("Everyone"))
FolderAcl.PurgeAccessRules(New Principal.NTAccount("Administrators"))
FolderAcl.PurgeAccessRules(New Principal.NTAccount("Creator Owner"))
FolderAcl.PurgeAccessRules(New Principal.NTAccount("SYSTEM"))
'FolderAcl.PurgeAccessRules(New Principal.NTAccount("pRQ-AdmNTPoste"))
FolderAcl.PurgeAccessRules(New Principal.NTAccount("Users"))
'Directory.SetCurrentDirectory("C:\BackgammonTxt\")
Dim Root As String = Directory.GetDirectoryRoot("C:\")
Dim Child As Boolean
If Root.Equals(path) Then
Child = False
Else
Child = True
End If
'Security enforcement
ApplySecurity(Directory.GetCurrentDirectory(), "Authenticated users", FileSystemRights.Read, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Authenticated users", FileSystemRights.WriteExtendedAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Authenticated users", FileSystemRights.CreateFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Authenticated users", FileSystemRights.WriteAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Authenticated users", FileSystemRights.WriteData, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Authenticated users", FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), UserAccount, FileSystemRights.Read, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), UserAccount, FileSystemRights.WriteExtendedAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), UserAccount, FileSystemRights.CreateFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), UserAccount, FileSystemRights.WriteAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), UserAccount, FileSystemRights.WriteData, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), UserAccount, FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Users", FileSystemRights.Read, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Users", FileSystemRights.WriteExtendedAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Users", FileSystemRights.CreateFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Users", FileSystemRights.WriteAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Users", FileSystemRights.WriteData, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Users", FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Everyone", FileSystemRights.Read, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Everyone", FileSystemRights.WriteExtendedAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Everyone", FileSystemRights.CreateFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Everyone", FileSystemRights.WriteAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Everyone", FileSystemRights.WriteData, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Everyone", FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Creator Owner", FileSystemRights.Read, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Creator Owner", FileSystemRights.WriteExtendedAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Creator Owner", FileSystemRights.CreateFiles, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Creator Owner", FileSystemRights.WriteAttributes, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Creator Owner", FileSystemRights.WriteData, AccessControlType.Allow, Child)
ApplySecurity(Directory.GetCurrentDirectory(), "Creator Owner", FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Allow, Child)
FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
FolderAcl.SetAccessRuleProtection(True, False)
End Sub
Code:
Public Sub CreateFileNew()
Dim AppDataFile As System.IO.StreamWriter
ChangeAllPermissions("C:\")
If Not System.IO.Directory.Exists("C:\BackgammonTxt") Then
System.IO.Directory.CreateDirectory("C:\BackgammonTxt")
ChangeAllPermissions("C:\BackgammonTxt")
'Dim FILE_NAME As String = "C:\BackgammonTxt\"
'MsgBox(FILE_NAME)
'If System.IO.File.Exists(FILE_NAME) = False Then
' System.IO.File.Create(FILE_NAME).Dispose()
'End If
'Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
'objWriter.Close()
System.IO.File.Create("C:\BackgammonTxt\AppDataFile.txt").Dispose()
ChangeAllPermissions("C:\BackgammonTxt\AppDataFile.txt")
Else
'System.IO.File.Delete("C:\BackgammonTxt")
'Read the file.
Dim fileContents = IO.File.ReadAllText("C:\BackgammonTxt\AppDataFile.txt")
'Clear the file.
IO.File.WriteAllText("C:\BackgammonTxt\AppDataFile.txt", String.Empty)
End If
Dim FolderPath As String = "C:\BackgammonTxt" 'Specify the folder here
Dim UserAccount As String = GetUserName() 'Specify the user here
Dim FolderInfo As New IO.DirectoryInfo(FolderPath)
Dim FolderAcl As New DirectorySecurity
'Dim userfldr As New UserFolder("C:\BackgammonTxt", UserAccount)
'userfldr.FixAcl(True)
FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
FolderAcl.AddAccessRule(New FileSystemAccessRule("Authenticated Users", FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
'FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
'FolderAcl.SetAccessRuleProtection(True, False) 'uncomment to remove existing permissions
FolderInfo.SetAccessControl(FolderAcl)
'MsgBox("Username " & GetUserName())
'MyAPI.SetFolderPermissions("C:\BackgammonTxt\AppDataFile.txt", GetUserName, True)
AppDataFile =
My.Computer.FileSystem.OpenTextFileWriter("C:\BackgammonTxt\AppDataFile.txt", True)
'έτσι γίνεται κρυφός ο φάκελος και το αρχείο
'IO.File.SetAttributes("c:\BackgammonTxt", IO.FileAttributes.Hidden Or
' IO.FileAttributes.System)
IO.File.SetAttributes("C:\BackgammonTxt", IO.FileAttributes.Normal)
'AppDataFile.Close()
'My.Computer.FileSystem.OpenTextFileWriter("c:\AppDataFile.txt", True, System.Text.Encoding.Unicode)
'My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath & "\AppDataFile.txt", True)
WriteToFile(AppDataFile)
End Sub
Code:
Public Sub ApplySecurity(ByVal file As String, ByVal user As String, ByVal rights As FileSystemRights, ByVal controlType As AccessControlType, ByVal isChild As Boolean)
Dim dInfo As DirectoryInfo
Dim dSecurity As DirectorySecurity
If Directory.Exists(file) Then
'Retrieving directory info
dInfo = New DirectoryInfo(file)
'Retrieving Directory Access Controls
dSecurity = dInfo.GetAccessControl()
'Applying a Directory Access Rule
Try
dSecurity.SetAccessRuleProtection(True, True)
If isChild Then
'If we pass true in isChild, we activate the inheritance of container or object rights
dSecurity.AddAccessRule(New FileSystemAccessRule(user, rights, InheritanceFlags.ObjectInherit Or InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, controlType))
dInfo.SetAccessControl(dSecurity)
Else
dSecurity.AddAccessRule(New FileSystemAccessRule(user, rights, InheritanceFlags.None, PropagationFlags.None, controlType))
dInfo.SetAccessControl(dSecurity)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub