Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="withio.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// <Snippet1>
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.
// </Snippet1>
1 change: 1 addition & 0 deletions xml/System/UnauthorizedAccessException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
The following example illustrates the <xref:System.UnauthorizedAccessException> 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":::

]]></format>
Expand Down