title: dotnet 6.0 C# - Access to path denied
body: <p>I am receiving an UnauthorizedAccessException when trying to open a file in my home directory which was initially created using the code shown below.</p>
  <p>The initial run of this program will create the empty file &quot;file.txt&quot; in my home directory without issue regardless of the File.Open option (read-only or write) selected.  The code will also run when the file already exists and I select the read-only option.</p>
  <p>I have checked the files permissions and I have full read/write access to the file and directory.  My user is listed as the owner of the file.  I have tried a different directory (C:\) and get the same error.</p>
  <p>The program was written in C#10.0 using the latest version of VS Code and the .Net compiler 4.0.0-6.21526.21.</p>
  <p>Error Text:</p>
  <pre><code>Press R for read-only or W for writeable: w
  System.UnauthorizedAccessException says Access to the path 'C:\Users\johnc\Documents\Repositories\dotnet\C#10andDotNet6Book\Chapter03\file.txt' is denied.
  System.UnauthorizedAccessException says
  System.UnauthorizedAccessException says System.Private.CoreLib
  System.UnauthorizedAccessException says    at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
     at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
     at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
     at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
     at System.IO.File.Open(String path, FileMode mode, FileAccess access)
     at Program.&lt;Main&gt;$(String[] args) in C:\Users\johnc\Documents\Repositories\dotnet\C#10andDotNet6Book\Chapter03\SelectionStatements\Program.cs:line 19
  System.UnauthorizedAccessException says Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions)
  </code></pre>
  <p>dotnet --info:</p>
  <pre><code>.NET SDK (reflecting any global.json):
   Version:   6.0.100
   Commit:    9e8b04bbff
  
  Runtime Environment:
   OS Name:     Windows
   OS Version:  10.0.19042
   OS Platform: Windows
   RID:         win10-x64
   Base Path:   C:\Program Files\dotnet\sdk\6.0.100\
  
  Host (useful for support):
    Version: 6.0.0
    Commit:  4822e3c3aa
  .NET SDKs installed:
    3.1.415 [C:\Program Files\dotnet\sdk]
    5.0.403 [C:\Program Files\dotnet\sdk]
    6.0.100 [C:\Program Files\dotnet\sdk]
  
  .NET runtimes installed:
    Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  </code></pre>
  <p>Global Usings:</p>
  <pre><code>// &lt;auto-generated/&gt;
  global using global::System;
  global using global::System.Collections.Generic;
  global using global::System.IO;
  global using global::System.Linq;
  global using global::System.Net.Http;
  global using global::System.Threading;
  global using global::System.Threading.Tasks;
  </code></pre>
  <p>Code:</p>
  <pre><code>using static System.Console;
  
  string path = $@&quot;{Environment.GetFolderPath(Environment.SpecialFolder.Personal)}\Repositories\dotnet\C#10andDotNet6Book\Chapter03&quot;;
  
  Write(&quot;Press R for read-only or W for writeable: &quot;);
  ConsoleKeyInfo key = ReadKey();
  WriteLine();
  
  Stream? s;
  
  try
  {
      if (key.Key == ConsoleKey.R)
      {
          s = File.Open(Path.Combine(path, &quot;file.txt&quot;), FileMode.OpenOrCreate, FileAccess.Read);
      }
      else
      {
          s = File.Open(Path.Combine(path, &quot;file.txt&quot;), FileMode.OpenOrCreate, FileAccess.Write);
      }
  
      string message;
  
      switch (s)
      {
          case FileStream writeableFile when s.CanWrite:
              message = &quot;The stream is a file that I can write to.&quot;;
              break;
          case FileStream readOnlyFile:
              message = &quot;The stream is a read-only file.&quot;;
              break;
          case MemoryStream ms:
              message = &quot;The stream is a memory address.&quot;;
              break;
          default:
              message = &quot;The stream is some other type.&quot;;
              break;
          case null:
              message = &quot;The stream is null.&quot;;
              break;
      }
  
      WriteLine(message);
  }
  catch (Exception ex)
  {
      WriteLine($&quot;{ex.GetType()} says {ex.Message}&quot;);
      WriteLine($&quot;{ex.GetType()} says {ex.HelpLink}&quot;);
      WriteLine($&quot;{ex.GetType()} says {ex.Source}&quot;);
      WriteLine($&quot;{ex.GetType()} says {ex.StackTrace}&quot;);
      WriteLine($&quot;{ex.GetType()} says {ex.TargetSite}&quot;);
  }
  </code></pre>