From 7ae86639f377619e3847926d1b8c3a862bbba607 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Sun, 8 May 2022 12:10:51 -0700 Subject: [PATCH] UnauthorizedAccessException F# snippets --- .../Overview/fs.fsproj | 10 ++++++++ .../Overview/withio.fs | 23 +++++++++++++++++++ xml/System/UnauthorizedAccessException.xml | 1 + 3 files changed, 34 insertions(+) create mode 100644 snippets/fsharp/System/UnauthorizedAccessException/Overview/fs.fsproj create mode 100644 snippets/fsharp/System/UnauthorizedAccessException/Overview/withio.fs diff --git a/snippets/fsharp/System/UnauthorizedAccessException/Overview/fs.fsproj b/snippets/fsharp/System/UnauthorizedAccessException/Overview/fs.fsproj new file mode 100644 index 00000000000..21605144bc5 --- /dev/null +++ b/snippets/fsharp/System/UnauthorizedAccessException/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/UnauthorizedAccessException/Overview/withio.fs b/snippets/fsharp/System/UnauthorizedAccessException/Overview/withio.fs new file mode 100644 index 00000000000..2de57b73a2f --- /dev/null +++ b/snippets/fsharp/System/UnauthorizedAccessException/Overview/withio.fs @@ -0,0 +1,23 @@ +// +open System +open System.IO + + +let filePath = @".\ROFile.txt" +if File.Exists filePath |> not then + File.Create filePath |> ignore +// Keep existing attributes, and set ReadOnly attribute. +File.SetAttributes(filePath, (FileInfo filePath).Attributes ||| FileAttributes.ReadOnly) + +do + use sw = new StreamWriter(filePath) + try + sw.Write "Test" + with :? UnauthorizedAccessException -> + let attr = (FileInfo filePath).Attributes + printf "UnAuthorizedAccessException: Unable to access file. " + if int (attr &&& FileAttributes.ReadOnly) > 0 then + printf "The file is read-only." +// The example displays the following output: +// UnAuthorizedAccessException: Unable to access file. The file is read-only. +// \ No newline at end of file diff --git a/xml/System/UnauthorizedAccessException.xml b/xml/System/UnauthorizedAccessException.xml index 2d6181a5af1..aed85af63e0 100644 --- a/xml/System/UnauthorizedAccessException.xml +++ b/xml/System/UnauthorizedAccessException.xml @@ -73,6 +73,7 @@ The following example illustrates the exception that is thrown when attempting to write to a read-only file. :::code language="csharp" source="~/snippets/csharp/System/UnauthorizedAccessException/Overview/withio.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/UnauthorizedAccessException/Overview/withio.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.unauthorizedaccessexception/vb/withio.vb" id="Snippet1"::: ]]>