Skip to content

Commit 48f758e

Browse files
carlossanlopmairaw
andauthored
Add usage examples for the new FileSystemAclExtensions file and directory creation methods (#3781)
* Add usage examples for the new FileSystemAclExtensions file and directory creation methods * suggestions by Thraka * suggestions by mairaw Co-Authored-By: Maira Wenzel <[email protected]> * Additional comments for legacy methods Co-authored-by: Maira Wenzel <[email protected]>
1 parent cf66974 commit 48f758e

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed

xml/System.IO/DirectoryInfo.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,35 @@
275275
276276
For a list of common I/O tasks, see [Common I/O Tasks](~/docs/standard/io/common-i-o-tasks.md).
277277
278+
> [!IMPORTANT]
279+
> This method was ported to .NET Core 3.1 as an extension method of the `FileSystemAclExtensions` class as part of the `System.Security.AccessControl` assembly: <xref:System.IO.FileSystemAclExtensions.Create(System.IO.DirectoryInfo,System.Security.AccessControl.DirectorySecurity)>.
280+
281+
## Examples
282+
283+
The following code example creates a new directory inside the user's temporary folder with the specified directory security attributes:
284+
285+
```csharp
286+
using System.IO;
287+
using System.Security.AccessControl;
288+
using System.Security.Principal;
289+
namespace ConsoleApp
290+
{
291+
class Program
292+
{
293+
static void Main()
294+
{
295+
DirectorySecurity security = new DirectorySecurity();
296+
SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
297+
FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
298+
security.AddAccessRule(accessRule);
299+
string path = Path.Combine(Path.GetTempPath(), "directoryToCreate");
300+
DirectoryInfo dirInfo = new DirectoryInfo(path);
301+
dirInfo.Create(security);
302+
}
303+
}
304+
}
305+
```
306+
278307
]]></format>
279308
</remarks>
280309
<exception cref="T:System.IO.IOException">The directory specified by <paramref name="path" /> is read-only or is not empty.</exception>

xml/System.IO/File.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,9 @@
12221222
12231223
For a list of common I/O tasks, see [Common I/O Tasks](~/docs/standard/io/common-i-o-tasks.md).
12241224
1225+
> [!IMPORTANT]
1226+
> This method was ported to .NET Core 3.1 in the following form: <xref:System.IO.FileSystemAclExtensions.Create(System.IO.FileInfo,System.IO.FileMode,System.Security.AccessControl.FileSystemRights,System.IO.FileShare,System.Int32,System.IO.FileOptions,System.Security.AccessControl.FileSecurity)>.
1227+
12251228
]]></format>
12261229
</remarks>
12271230
<exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.

xml/System.IO/FileStream.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,8 @@
15421542
15431543
For a list of common file and directory operations, see [Common I/O Tasks](~/docs/standard/io/common-i-o-tasks.md).
15441544
1545-
1545+
> [!IMPORTANT]
1546+
> This constructor does not exist in .NET Core. Instead, starting in .NET Core 3.1, you can use the following extension method of the `FileSystemAclExtensions` class inside the `System.Security.AccessControl` assembly: <xref:System.IO.FileSystemAclExtensions.Create(System.IO.FileInfo,System.IO.FileMode,System.Security.AccessControl.FileSystemRights,System.IO.FileShare,System.Int32,System.IO.FileOptions,System.Security.AccessControl.FileSecurity)>.
15461547
15471548
## Examples
15481549
The following example writes data to a file and then reads the data using the <xref:System.IO.FileStream> object.

xml/System.IO/FileSystemAclExtensions.xml

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,38 @@
5454
5555
## Remarks
5656
57-
This extension method was added to .NET Core to bring the functionality that was provided by the `System.IO.DirectoryInfo.Create(System.Security.AccessControl.DirectorySecurity)` .NET Framework method.
57+
This extension method was added to .NET Core to bring the functionality that was provided by the [DirectoryInfo.Create(DirectorySecurity)](https://docs.microsoft.com/dotnet/api/system.io.directoryinfo.create?view=netframework-4.8#System_IO_DirectoryInfo_Create_System_Security_AccessControl_DirectorySecurity_) .NET Framework method.
58+
59+
## Examples
60+
61+
The following code example creates a new directory inside the user's temporary folder with the specified directory security attributes:
62+
63+
```csharp
64+
using System.IO;
65+
using System.Security.AccessControl;
66+
using System.Security.Principal;
67+
68+
namespace ConsoleApp
69+
{
70+
class Program
71+
{
72+
static void Main()
73+
{
74+
DirectorySecurity security = new DirectorySecurity();
75+
76+
SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
77+
FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
78+
security.AddAccessRule(accessRule);
79+
80+
string path = Path.Combine(Path.GetTempPath(), "directoryToCreate");
81+
82+
DirectoryInfo dirInfo = new DirectoryInfo(path);
83+
84+
dirInfo.Create(security);
85+
}
86+
}
87+
}
88+
```
5889
5990
]]></format>
6091
</remarks>
@@ -102,7 +133,41 @@ This extension method was added to .NET Core to bring the functionality that was
102133
103134
## Remarks
104135
105-
This extension method was added to .NET Core to bring the functionality that was provided by the `System.IO.FileStream.#ctor(System.String,System.IO.FileMode,System.Security.AccessControl.FileSystemRights,System.IO.FileShare,System.Int32,System.IO.FileOptions,System.Security.AccessControl.FileSecurity)` .NET Framework constructor.
136+
This extension method was added to .NET Core to bring the functionality that was provided by:
137+
138+
- The [FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)](https://docs.microsoft.com/dotnet/api/system.io.filestream.-ctor?view=netframework-4.8#System_IO_FileStream__ctor_System_String_System_IO_FileMode_System_Security_AccessControl_FileSystemRights_System_IO_FileShare_System_Int32_System_IO_FileOptions_System_Security_AccessControl_FileSecurity_) .NET Framework constructor.
139+
- The [File.Create(String, Int32, FileOptions, FileSecurity](https://docs.microsoft.com/en-us/dotnet/api/system.io.file.create?view=netframework-4.8#System_IO_File_Create_System_String_System_Int32_System_IO_FileOptions_System_Security_AccessControl_FileSecurity_) .NET Framework method.
140+
141+
## Examples
142+
143+
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:
144+
145+
```csharp
146+
using System.IO;
147+
using System.Security.AccessControl;
148+
using System.Security.Principal;
149+
150+
namespace ConsoleApp
151+
{
152+
class Program
153+
{
154+
static void Main()
155+
{
156+
FileSecurity security = new FileSecurity();
157+
158+
SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
159+
FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
160+
security.AddAccessRule(accessRule);
161+
162+
string path = Path.Combine(Path.GetTempPath(), "fileToCreate.txt");
163+
164+
FileInfo fileInfo = new FileInfo(path);
165+
166+
fileInfo.Create(FileMode.Create, FileSystemRights.FullControl, FileShare.Read, 4096, FileOptions.None, security);
167+
}
168+
}
169+
}
170+
```
106171
107172
]]></format>
108173
</remarks>

0 commit comments

Comments
 (0)