File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed
snippets/fsharp/System/UnauthorizedAccessException/Overview Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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>
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments