c# File.WriteAllBytes 引发“访问被拒绝”异常,而“FileStream.Write”则为成功

发布于 2025-01-14 02:06:35 字数 1437 浏览 2 评论 0原文

我有以下代码:

var cnt = new byte[] { 0xaa, 0xbb };
var fileName = @"C:\ProgramData\path\to\myfile.bin";

try
{
    File.WriteAllBytes(fileName, cnt);
    Console.WriteLine(("WriteAllBytes OK"));
}
catch (Exception ex)
{
    Console.WriteLine("WriteAllBytes Exception: " + ex.Message);
}

try
{
    using (var f = new FileStream(fileName, FileMode.Open))
    {
        f.Write(cnt, 0, 2);
        f.Close();
        Console.WriteLine("FileStream.Write OK");
    }
}
catch (Exception ex)
{
    Console.WriteLine("FileStream.Write Exception: " + ex.Message);
}

此代码给出以下输出:

WriteAllBytes Exception: Access denied to path 'C:\ProgramData\path\to\myfile.bin'.
FileStream.Write OK

我向文件权限添加了“每个人/完全控制”,并将文件所有权分配给 MYPC\MyUser。

我使用 Process Monitor 来查看发生了什么,这是输出:

CreateFile C:\ProgramData\Path\to\myfile.bin 访问被拒绝所需访问:通用写入、读取属性、处置:OverwriteIf、选项:同步 IO 非警报、非目录文件、打开不调用、属性:不适用,共享模式:读取,分配大小:0 MYPC\MyUser 00000000:0002d238

CreateFile C:\ProgramData\Path\to\myfile.bin 成功 所需访问:通用读/写,处置:打开,选项:同步 IO 非警报、非目录文件、打开不召回、属性:n/ a、ShareMode:读取、AllocationSize:不适用、OpenResult:打开 MYPC\MyUser 00000000:0002d238 WriteFile C:\ProgramData\Path\to\myfile.bin SUCCESS 偏移量:0,长度:2,优先级:正常 MYPC\MyUser 00000000:0002d238 CloseFile C:\ProgramData\Path\to\myfile.bin 成功 MYPC\MyUser 00000000:0002d238

我不明白为什么在这种情况下 WriteToFile 会失败。

I have the following code:

var cnt = new byte[] { 0xaa, 0xbb };
var fileName = @"C:\ProgramData\path\to\myfile.bin";

try
{
    File.WriteAllBytes(fileName, cnt);
    Console.WriteLine(("WriteAllBytes OK"));
}
catch (Exception ex)
{
    Console.WriteLine("WriteAllBytes Exception: " + ex.Message);
}

try
{
    using (var f = new FileStream(fileName, FileMode.Open))
    {
        f.Write(cnt, 0, 2);
        f.Close();
        Console.WriteLine("FileStream.Write OK");
    }
}
catch (Exception ex)
{
    Console.WriteLine("FileStream.Write Exception: " + ex.Message);
}

this code gives the following output:

WriteAllBytes Exception: Access denied to path 'C:\ProgramData\path\to\myfile.bin'.
FileStream.Write OK

I added Everyone/Full control to file permissions and assigned file ownership to MYPC\MyUser.

I used Process Monitor to see what's going on, this is the output:

CreateFile C:\ProgramData\Path\to\myfile.bin ACCESS DENIED Desired Access: Generic Write, Read Attributes, Disposition: OverwriteIf, Options: Synchronous IO Non-Alert, Non-Directory File, Open No Recall, Attributes: n/a, ShareMode: Read, AllocationSize: 0 MYPC\MyUser 00000000:0002d238

CreateFile C:\ProgramData\Path\to\myfile.bin SUCCESS Desired Access: Generic Read/Write, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Open No Recall, Attributes: n/a, ShareMode: Read, AllocationSize: n/a, OpenResult: Opened MYPC\MyUser 00000000:0002d238
WriteFile C:\ProgramData\Path\to\myfile.bin SUCCESS Offset: 0, Length: 2, Priority: Normal MYPC\MyUser 00000000:0002d238
CloseFile C:\ProgramData\Path\to\myfile.bin SUCCESS MYPC\MyUser 00000000:0002d238

I don't understand why in this situation WriteToFile fails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文