Skip to content

Commit b0c8135

Browse files
authored
Update GetAccessControl/SetAccessControl docs (dotnet#10005)
1 parent 3929508 commit b0c8135

File tree

21 files changed

+542
-573
lines changed

21 files changed

+542
-573
lines changed

snippets/csharp/System.IO/Directory/GetAccessControl/Project.csproj

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0-windows</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

snippets/csharp/System.IO/Directory/GetAccessControl/sample.cs renamed to snippets/csharp/System.IO/DirectoryInfo/GetAccessControl/sample.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ public static void Main()
3434
}
3535

3636
// Adds an ACL entry on the specified directory for the specified account.
37-
public static void AddDirectorySecurity(string DirectoryName, string Account, FileSystemRights Rights, AccessControlType ControlType)
37+
public static void AddDirectorySecurity(
38+
string DirectoryName,
39+
string Account,
40+
FileSystemRights Rights,
41+
AccessControlType ControlType
42+
)
3843
{
3944
// Create a new DirectoryInfo object.
40-
DirectoryInfo dInfo = new DirectoryInfo(DirectoryName);
45+
DirectoryInfo dInfo = new(DirectoryName);
4146

4247
// Get a DirectorySecurity object that represents the
4348
// current security settings.
@@ -53,10 +58,15 @@ public static void AddDirectorySecurity(string DirectoryName, string Account, Fi
5358
}
5459

5560
// Removes an ACL entry on the specified directory for the specified account.
56-
public static void RemoveDirectorySecurity(string DirectoryName, string Account, FileSystemRights Rights, AccessControlType ControlType)
61+
public static void RemoveDirectorySecurity(
62+
string DirectoryName,
63+
string Account,
64+
FileSystemRights Rights,
65+
AccessControlType ControlType
66+
)
5767
{
5868
// Create a new DirectoryInfo object.
59-
DirectoryInfo dInfo = new DirectoryInfo(DirectoryName);
69+
DirectoryInfo dInfo = new(DirectoryName);
6070

6171
// Get a DirectorySecurity object that represents the
6272
// current security settings.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Library</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0-windows</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

snippets/csharp/System.IO/FileInfo/GetAccessControl/sample.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ public static void Main()
3838
}
3939

4040
// Adds an ACL entry on the specified file for the specified account.
41-
public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
41+
public static void AddFileSecurity(
42+
string FileName,
43+
string Account,
44+
FileSystemRights Rights,
45+
AccessControlType ControlType
46+
)
4247
{
4348
// Create a new FileInfo object.
44-
FileInfo fInfo = new FileInfo(FileName);
49+
FileInfo fInfo = new(FileName);
4550

4651
// Get a FileSecurity object that represents the
4752
// current security settings.
@@ -57,10 +62,15 @@ public static void AddFileSecurity(string FileName, string Account, FileSystemRi
5762
}
5863

5964
// Removes an ACL entry on the specified file for the specified account.
60-
public static void RemoveFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
65+
public static void RemoveFileSecurity(
66+
string FileName,
67+
string Account,
68+
FileSystemRights Rights,
69+
AccessControlType ControlType
70+
)
6171
{
6272
// Create a new FileInfo object.
63-
FileInfo fInfo = new FileInfo(FileName);
73+
FileInfo fInfo = new(FileName);
6474

6575
// Get a FileSecurity object that represents the
6676
// current security settings.
@@ -83,4 +93,4 @@ public static void RemoveFileSecurity(string FileName, string Account, FileSyste
8393
//Removing access control entry from c:\test.xml
8494
//Done.
8595
//
86-
//</SNIPPET1>
96+
//</SNIPPET1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0-windows</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

snippets/csharp/System.IO/File/GetAccessControl/sample.cs renamed to snippets/csharp/System.IO/FileSystemAclExtensions/GetAccessControl/sample.cs

+9-15
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ public static void Main()
1313
{
1414
string fileName = "test.xml";
1515

16-
Console.WriteLine("Adding access control entry for "
17-
+ fileName);
16+
Console.WriteLine($"Adding access control entry for {fileName}");
1817

1918
// Add the access control entry to the file.
2019
AddFileSecurity(fileName, @"DomainName\AccountName",
2120
FileSystemRights.ReadData, AccessControlType.Allow);
2221

23-
Console.WriteLine("Removing access control entry from "
24-
+ fileName);
22+
Console.WriteLine($"Removing access control entry from {fileName}");
2523

2624
// Remove the access control entry from the file.
2725
RemoveFileSecurity(fileName, @"DomainName\AccountName",
@@ -39,35 +37,31 @@ public static void Main()
3937
public static void AddFileSecurity(string fileName, string account,
4038
FileSystemRights rights, AccessControlType controlType)
4139
{
42-
43-
// Get a FileSecurity object that represents the
44-
// current security settings.
45-
FileSecurity fSecurity = File.GetAccessControl(fileName);
40+
FileInfo fileInfo = new(fileName);
41+
FileSecurity fSecurity = fileInfo.GetAccessControl();
4642

4743
// Add the FileSystemAccessRule to the security settings.
4844
fSecurity.AddAccessRule(new FileSystemAccessRule(account,
4945
rights, controlType));
5046

5147
// Set the new access settings.
52-
File.SetAccessControl(fileName, fSecurity);
48+
fileInfo.SetAccessControl(fSecurity);
5349
}
5450

5551
// Removes an ACL entry on the specified file for the specified account.
5652
public static void RemoveFileSecurity(string fileName, string account,
5753
FileSystemRights rights, AccessControlType controlType)
5854
{
59-
60-
// Get a FileSecurity object that represents the
61-
// current security settings.
62-
FileSecurity fSecurity = File.GetAccessControl(fileName);
55+
FileInfo fileInfo = new(fileName);
56+
FileSecurity fSecurity = fileInfo.GetAccessControl();
6357

6458
// Remove the FileSystemAccessRule from the security settings.
6559
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
6660
rights, controlType));
6761

6862
// Set the new access settings.
69-
File.SetAccessControl(fileName, fSecurity);
63+
fileInfo.SetAccessControl(fSecurity);
7064
}
7165
}
7266
}
73-
//</SNIPPET1>
67+
//</SNIPPET1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0-windows</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
+14-21
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Imports System.IO
33
Imports System.Security.AccessControl
44

5-
6-
75
Module FileExample
86

97
Sub Main()
@@ -13,13 +11,13 @@ Module FileExample
1311
Console.WriteLine("Adding access control entry for " & fileName)
1412

1513
' Add the access control entry to the file.
16-
AddFileSecurity(fileName, "DomainName\AccountName", _
14+
AddFileSecurity(fileName, "DomainName\AccountName",
1715
FileSystemRights.ReadData, AccessControlType.Allow)
1816

1917
Console.WriteLine("Removing access control entry from " & fileName)
2018

2119
' Remove the access control entry from the file.
22-
RemoveFileSecurity(fileName, "DomainName\AccountName", _
20+
RemoveFileSecurity(fileName, "DomainName\AccountName",
2321
FileSystemRights.ReadData, AccessControlType.Allow)
2422

2523
Console.WriteLine("Done.")
@@ -29,42 +27,37 @@ Module FileExample
2927

3028
End Sub
3129

32-
3330
' Adds an ACL entry on the specified file for the specified account.
34-
Sub AddFileSecurity(ByVal fileName As String, ByVal account As String, _
31+
Sub AddFileSecurity(ByVal fileName As String, ByVal account As String,
3532
ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
36-
37-
' Get a FileSecurity object that represents the
38-
' current security settings.
39-
Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)
33+
34+
Dim fileInfo As New FileInfo(fileName)
35+
Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()
4036

4137
' Add the FileSystemAccessRule to the security settings.
42-
Dim accessRule As FileSystemAccessRule = _
43-
New FileSystemAccessRule(account, rights, controlType)
38+
Dim accessRule As New FileSystemAccessRule(account, rights, controlType)
4439

4540
fSecurity.AddAccessRule(accessRule)
4641

4742
' Set the new access settings.
48-
File.SetAccessControl(fileName, fSecurity)
43+
fileInfo.SetAccessControl(fSecurity)
4944

5045
End Sub
5146

52-
5347
' Removes an ACL entry on the specified file for the specified account.
54-
Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String, _
48+
Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String,
5549
ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
5650

57-
' Get a FileSecurity object that represents the
58-
' current security settings.
59-
Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)
51+
Dim fileInfo As New FileInfo(fileName)
52+
Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()
6053

6154
' Remove the FileSystemAccessRule from the security settings.
62-
fSecurity.RemoveAccessRule(New FileSystemAccessRule(account, _
55+
fSecurity.RemoveAccessRule(New FileSystemAccessRule(account,
6356
rights, controlType))
6457

6558
' Set the new access settings.
66-
File.SetAccessControl(fileName, fSecurity)
59+
fileInfo.SetAccessControl(fSecurity)
6760

6861
End Sub
6962
End Module
70-
'</SNIPPET1>
63+
'</SNIPPET1>

0 commit comments

Comments
 (0)