Skip to content

Commit b69eed3

Browse files
Updating FileSystem.AccessControl examples (dotnet#5247)
* Updating FileSystem.AccessControl examples - Make sure the samples are using the latest .NET 5.0 features - Move them to sample files instead of having them directly written in the remarks * Apply suggestions from code review Co-authored-by: Adam Sitnik <[email protected]> Co-authored-by: Adam Sitnik <[email protected]>
1 parent d638882 commit b69eed3

File tree

3 files changed

+91
-53
lines changed

3 files changed

+91
-53
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.IO;
2+
using System.Runtime.Versioning;
3+
using System.Security.AccessControl;
4+
using System.Security.Principal;
5+
6+
namespace MyNamespace
7+
{
8+
public class MyClassCS
9+
{
10+
// Attribute to address CA1416 warning:
11+
// System.IO.FileSystem.AccessControl APIs are only available on Windows
12+
[SupportedOSPlatform("windows")]
13+
static void Main()
14+
{
15+
// Create the file security object
16+
17+
SecurityIdentifier identity = new SecurityIdentifier(
18+
WellKnownSidType.BuiltinUsersSid, // This maps to "Everyone" user group in Windows
19+
null); // null is OK for this particular user group. For others, a non-empty value might be required
20+
FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
21+
22+
DirectorySecurity expectedSecurity = new DirectorySecurity();
23+
expectedSecurity.AddAccessRule(accessRule);
24+
25+
// Make sure the directory does not exist, then create it
26+
27+
string dirPath = Path.Combine(Path.GetTempPath(), "directoryToCreate");
28+
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
29+
if (dirInfo.Exists)
30+
{
31+
dirInfo.Delete(recursive: true);
32+
}
33+
34+
dirInfo.Create(expectedSecurity);
35+
}
36+
}
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.Versioning;
4+
using System.Security.AccessControl;
5+
using System.Security.Principal;
6+
using System.Text;
7+
8+
namespace MyNamespace
9+
{
10+
public class MyClassCS
11+
{
12+
// Attribute to address CA1416 warning:
13+
// System.IO.FileSystem.AccessControl APIs are only available on Windows
14+
[SupportedOSPlatform("windows")]
15+
static void Main()
16+
{
17+
// Create the file security object
18+
19+
var identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
20+
var accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
21+
22+
var security = new FileSecurity();
23+
security.AddAccessRule(accessRule);
24+
25+
// Make sure the file does not exist, or FileMode.CreateNew will throw
26+
27+
string filePath = Path.Combine(Path.GetTempPath(), "temp.txt");
28+
var fileInfo = new FileInfo(filePath);
29+
if (fileInfo.Exists)
30+
{
31+
fileInfo.Delete();
32+
}
33+
34+
// Create the file with the specified security and write some text
35+
36+
using (FileStream stream = fileInfo.Create(
37+
FileMode.CreateNew,
38+
FileSystemRights.FullControl,
39+
FileShare.ReadWrite,
40+
4096, // Default buffer size
41+
FileOptions.None,
42+
security))
43+
{
44+
string text = "Hello world!";
45+
byte[] writeBuffer = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true).GetBytes(text);
46+
47+
stream.Write(writeBuffer, 0, writeBuffer.Length);
48+
} // Dispose flushes the file to disk
49+
}
50+
}
51+
}

xml/System.IO/FileSystemAclExtensions.xml

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,7 @@ This extension method was added to .NET Core to bring the functionality that was
6464
6565
The following code example creates a new directory inside the user's temporary folder with the specified directory security attributes:
6666
67-
```csharp
68-
using System.IO;
69-
using System.Security.AccessControl;
70-
using System.Security.Principal;
71-
72-
namespace ConsoleApp
73-
{
74-
class Program
75-
{
76-
static void Main()
77-
{
78-
DirectorySecurity security = new DirectorySecurity();
79-
80-
SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
81-
FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
82-
security.AddAccessRule(accessRule);
83-
84-
string path = Path.Combine(Path.GetTempPath(), "directoryToCreate");
85-
86-
DirectoryInfo dirInfo = new DirectoryInfo(path);
87-
88-
dirInfo.Create(security);
89-
}
90-
}
91-
}
92-
```
67+
[!code-csharp[DirectoryInfoCreateDirectory](~/samples/snippets/csharp/VS_Snippets_CLR/IO.FileSystem.AccessControl/DirectoryInfoCreateDirectory.cs)]
9368
9469
]]></format>
9570
</remarks>
@@ -146,34 +121,9 @@ This extension method was added to .NET Core to bring the functionality that was
146121
147122
## Examples
148123
149-
The following code example creates a new text file (with the default buffer size of 4096) inside the user's temporary folder with the specified file security attributes:
150-
151-
```csharp
152-
using System.IO;
153-
using System.Security.AccessControl;
154-
using System.Security.Principal;
155-
156-
namespace ConsoleApp
157-
{
158-
class Program
159-
{
160-
static void Main()
161-
{
162-
FileSecurity security = new FileSecurity();
163-
164-
SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
165-
FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
166-
security.AddAccessRule(accessRule);
167-
168-
string path = Path.Combine(Path.GetTempPath(), "fileToCreate.txt");
169-
170-
FileInfo fileInfo = new FileInfo(path);
124+
The following code example creates a new text file inside the user's temporary folder, explicitly specifying all security attributes:
171125
172-
fileInfo.Create(FileMode.Create, FileSystemRights.FullControl, FileShare.Read, 4096, FileOptions.None, security);
173-
}
174-
}
175-
}
176-
```
126+
[!code-csharp[FileInfoCreate](~/samples/snippets/csharp/VS_Snippets_CLR/IO.FileSystem.AccessControl/FileInfoCreate.cs)]
177127
178128
]]></format>
179129
</remarks>

0 commit comments

Comments
 (0)