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"::: ]]>