Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 20, 2025

Summary

Fixes the confusing and incorrect UnauthorizedAccessException documentation for File.ReadAllText and File.ReadAllLines methods that incorrectly stated these methods throw an exception when "path specified a file that is read-only."

Problem

The documentation for ReadAllText and ReadAllLines methods included this exception condition:

UnauthorizedAccessException: path specified a file that is read-only.

This is misleading and incorrect because:

  1. These methods only read from files using FileAccess.Read
  2. Reading from read-only files is perfectly valid and expected behavior
  3. The exception text was likely copy-pasted from write methods like File.WriteAllText where it makes sense

As noted in the .NET source code, File.ReadAllText opens files with:

  • FileMode.Open
  • FileAccess.Read
  • FileShare.Read

Changes

Removed the incorrect exception text from these four method overloads:

  • File.ReadAllText(string path)
  • File.ReadAllText(string path, Encoding encoding)
  • File.ReadAllLines(string path)
  • File.ReadAllLines(string path, Encoding encoding)

The UnauthorizedAccessException documentation now correctly lists only the valid conditions:

  • This operation is not supported on the current platform
  • path specified a directory
  • The caller does not have the required permission

Verification

Confirmed that all other File.Read* methods (ReadAllBytes, ReadAllBytesAsync, ReadAllLinesAsync, ReadAllTextAsync, ReadLines, ReadLinesAsync) already had correct documentation without this error.

Fixes #<issue_number>

Original prompt

This section details on the original issue you should resolve

<issue_title>Confusing exception on File.ReadAllText method: "UnauthorizedAccessException: path specified a file that is read-only."</issue_title>
<issue_description>### Type of issue

Missing information

Description

In the File.ReadAllText method description there's an exception listed:

UnauthorizedAccessException
path specified a file that is read-only.

Without further explanation to this exception it is very confusing to the reader: It implies that the File.ReadAllText cannot read from files that are read-only.

Looking at the .NET source code, however, File.ReadAllText opens files with arguments FileMode.Open, FileAccess.Read, FileShare.Read. So, it's perfectly legal – and reasonable, if I may say so – to read from read-only files using File.ReadAllText.

In the documentation, please

  1. elaborate on the fact that files are read with above mentioned file access arguments,
  2. elaborate on the (obscure) special case causing above mentioned UnauthorizedAccessException to be thrown.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalltext?view=net-9.0&devlangs=csharp&f1url=%3FappId%3DDev17IDEF1%26l%3DDE-DE%26k%3Dk(System.IO.File.ReadAllText)%3Bk(DevLang-csharp)%26rd%3Dtrue

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.IO/File.xml

Document Version Independent Id

b97f3644-3681-9fd1-a7b5-52e994db2233

Platform Id

01172106-1daf-bc5a-5bdc-75ec5f5ad0f5

Article author

@dotnet-bot</issue_description>

<agent_instructions>Remove incorrect statement from the File.ReadAllText method docs, make sure that none of the File.Read* methods repeat the same mistake.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@adamsitnik Hi @SetTrend

You are right, it's confusing and simply wrong. I suspect it's a copy-paste error from File.WriteAllText:

https://github.com/dotnet/runtime/blob/ef584b4c381c4e52fca7795c3b8cf108d5392315/src/libraries/System.Private.CoreLib/src/System/IO/File.cs#L648</comment_new>

Fixes #11913


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 20, 2025
Copilot AI changed the title [WIP] Fix confusing exception message for File.ReadAllText method Remove incorrect "read-only file" exception from File.Read* method documentation Oct 20, 2025
Copilot finished work on behalf of adamsitnik October 20, 2025 14:35
Copilot AI requested a review from adamsitnik October 20, 2025 14:35
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@adamsitnik adamsitnik added area-System.IO and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 20, 2025
@adamsitnik adamsitnik marked this pull request as ready for review October 20, 2025 15:34
@adamsitnik adamsitnik requested a review from a team as a code owner October 20, 2025 15:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR removes incorrect and misleading documentation about UnauthorizedAccessException from the File.ReadAllText and File.ReadAllLines methods. The documentation incorrectly stated these methods throw an exception when reading read-only files, which contradicts their actual behavior since they only perform read operations.

Key changes:

  • Removed the false exception condition "path specified a file that is read-only" from exception documentation
  • Applied fix consistently across all four affected method overloads

@gewarren gewarren merged commit 727a32e into main Oct 20, 2025
5 checks passed
@gewarren gewarren deleted the copilot/fix-file-readalltext-exception branch October 20, 2025 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confusing exception on File.ReadAllText method: "UnauthorizedAccessException: path specified a file that is read-only."

3 participants