Skip to content

Commit 535631d

Browse files
authored
UnauthorizedAccessException F# snippets (#8053)
1 parent b38f019 commit 535631d

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="withio.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// <Snippet1>
2+
open System
3+
open System.IO
4+
5+
6+
let filePath = @".\ROFile.txt"
7+
if File.Exists filePath |> not then
8+
File.Create filePath |> ignore
9+
// Keep existing attributes, and set ReadOnly attribute.
10+
File.SetAttributes(filePath, (FileInfo filePath).Attributes ||| FileAttributes.ReadOnly)
11+
12+
do
13+
use sw = new StreamWriter(filePath)
14+
try
15+
sw.Write "Test"
16+
with :? UnauthorizedAccessException ->
17+
let attr = (FileInfo filePath).Attributes
18+
printf "UnAuthorizedAccessException: Unable to access file. "
19+
if int (attr &&& FileAttributes.ReadOnly) > 0 then
20+
printf "The file is read-only."
21+
// The example displays the following output:
22+
// UnAuthorizedAccessException: Unable to access file. The file is read-only.
23+
// </Snippet1>

xml/System/UnauthorizedAccessException.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
The following example illustrates the <xref:System.UnauthorizedAccessException> exception that is thrown when attempting to write to a read-only file.
7474
7575
:::code language="csharp" source="~/snippets/csharp/System/UnauthorizedAccessException/Overview/withio.cs" id="Snippet1":::
76+
:::code language="fsharp" source="~/snippets/fsharp/System/UnauthorizedAccessException/Overview/withio.fs" id="Snippet1":::
7677
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.unauthorizedaccessexception/vb/withio.vb" id="Snippet1":::
7778
7879
]]></format>

0 commit comments

Comments
 (0)