【发布时间】:2022-01-15 20:34:39
【问题描述】:
当我尝试在我的主目录中打开一个文件时收到 UnauthorizedAccessException,该文件最初是使用下面显示的代码创建的。
该程序的初始运行将在我的主目录中创建空文件“file.txt”而不会出现问题,无论选择了 File.Open 选项(只读或写入)。当文件已经存在并且我选择只读选项时,代码也会运行。
我已经检查了文件权限,并且我对文件和目录具有完全的读/写权限。我的用户被列为文件的所有者。我尝试了不同的目录 (C:\) 并得到相同的错误。
该程序是使用最新版本的 VS Code 和 .Net 编译器 4.0.0-6.21526.21 用 C#10.0 编写的。
错误文本:
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.<Main>$(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)
dotnet --info:
.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]
全局使用:
// <auto-generated/>
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;
代码:
using static System.Console;
string path = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Personal)}\Repositories\dotnet\C#10andDotNet6Book\Chapter03";
Write("Press R for read-only or W for writeable: ");
ConsoleKeyInfo key = ReadKey();
WriteLine();
Stream? s;
try
{
if (key.Key == ConsoleKey.R)
{
s = File.Open(Path.Combine(path, "file.txt"), FileMode.OpenOrCreate, FileAccess.Read);
}
else
{
s = File.Open(Path.Combine(path, "file.txt"), FileMode.OpenOrCreate, FileAccess.Write);
}
string message;
switch (s)
{
case FileStream writeableFile when s.CanWrite:
message = "The stream is a file that I can write to.";
break;
case FileStream readOnlyFile:
message = "The stream is a read-only file.";
break;
case MemoryStream ms:
message = "The stream is a memory address.";
break;
default:
message = "The stream is some other type.";
break;
case null:
message = "The stream is null.";
break;
}
WriteLine(message);
}
catch (Exception ex)
{
WriteLine($"{ex.GetType()} says {ex.Message}");
WriteLine($"{ex.GetType()} says {ex.HelpLink}");
WriteLine($"{ex.GetType()} says {ex.Source}");
WriteLine($"{ex.GetType()} says {ex.StackTrace}");
WriteLine($"{ex.GetType()} says {ex.TargetSite}");
}
【问题讨论】:
-
您的代码对我来说运行良好。你检查目录权限了吗?
-
是的,一切似乎都是正确的。我的用户对以下目录和文件拥有完全权限。文件的所有权分配给我的用户。我可以使用文本编辑器(记事本)打开/更新/保存对文件的更改。有没有办法捕获 dotnet 编译器看到的权限?我想检查它是否看到我有写权限。
-
尝试在管理员模式下运行