.NET Framework 4.8应用在Windows Server 2019中部分扩展名文件访问被拒
C# (.NET Framework 4.8) 特定扩展名文件访问被拒异常
我在C#(.NET Framework 4.8)应用中碰到一个诡异的“访问被拒绝”问题:只有部分文件扩展名会触发这个错误,尤其是.txt和.csv,但.log、.json甚至.dll或.aaa这类扩展名的文件都能正常读取。
这个问题只出现在Windows Server 2019 Standard环境里,我的Windows 11开发环境完全没问题。我用File.ReadAllText读取文件,就算换成StreamReader或者尝试移动文件,依然会提示访问被拒。
我已经排除了实际权限问题:
- 我是机器管理员,就算以“管理员身份”运行应用,问题还是存在
- 文件夹和文件都给了“Everyone”完全权限
- 所有文件都是由同一个包含“SOME TEXT”的txt文件复制重命名而来,内容完全一致
- 我能用记事本打开、编辑保存、移动、重命名所有这些文件
我做了一个极简的窗体应用,包含两个按钮:
- 第一个按钮把文件夹里的文件列表加载到listBox中
- 第二个按钮把选中的文件内容加载到另一个textBox中,同时在日志textBox里记录操作信息
测试代码如下:
private void buttonList_Click(object sender, EventArgs e) { try { listBoxFileList.Items.Clear(); List<FileInfo> files = new DirectoryInfo(textBoxLoadFolder.Text) .GetFiles(textBoxfilemask.Text) .OrderBy(f => f.CreationTime) .ToList(); textBox1Log.Text += $@"File list loaded{Environment.NewLine}"; textBox1Log.Text += $@" -> found {files.Count} files{Environment.NewLine}"; files.ForEach(file => { listBoxFileList.Items.Add(file.FullName); textBox1Log.Text += $@" -> available file: {file.Name}{Environment.NewLine}"; }); } catch (Exception ex) { textBox1Log.Text += $@"ERROR loadin file list: {ex.Message}{Environment.NewLine}"; } } private void buttonReadFile_Click(object sender, EventArgs e) { textBoxFileContent.Text = ""; if (!(listBoxFileList.SelectedItem is null)) { string selectedFile = listBoxFileList.SelectedItem as string; try { textBox1Log.Text += $@"selected file: {selectedFile}{Environment.NewLine}"; textBox1Log.Text += $@"reading content....{Environment.NewLine}"; textBoxFileContent.Text = File.ReadAllText(selectedFile); textBox1Log.Text += $@"Done....{Environment.NewLine}"; } catch (Exception ex) { textBox1Log.Text += $@"ERROR loading file {selectedFile}: {ex.Message}{Environment.NewLine}"; } } else textBox1Log.Text += $@"WARNING No file selected{Environment.NewLine}"; }
日志输出
File list loaded -> found 7 files -> available file: RGP.txt -> available file: RGP.csv -> available file: RGP.dll -> available file: RGP.log -> available file: RGP.aaa -> available file: RGP.dat -> available file: RGP.json selected file: C:\ToLoad\RGP.log reading content.... Done.... selected file: C:\ToLoad\RGP.txt reading content.... ERROR loading file C:\ToLoad\RGP.txt: Access to the path 'C:\ToLoad\RGP.txt' is denied. selected file: C:\ToLoad\RGP.dat reading content.... Done....
补充测试(编辑#2)
我还发现无法创建.txt文件,但创建.tx文件是可以的。以下是创建文件时的异常堆栈跟踪:
ERROR creating file: Access to the path 'C:\ToLoad\NEW_test.txt' is denied. StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost) at System.IO.File.WriteAllText(String path, String contents) at TesterFilesaccess.Form1.button1_Click(Object sender, EventArgs e)
内容的提问来源于stack exchange,提问作者Carlo Pedroli
相关产品推荐
相关产品推荐

