gpt4 book ai didi

c# - 在 C#/Windows 中为文件模拟 'access denied'

转载 作者:可可西里 更新时间:2023-11-01 09:20:57 25 4
gpt4 key购买 nike

我正在尝试为读取不同文件的小型 C# 例程编写集成测试。

而且,好吧,我无意中想到进行测试会很棒,它指定了当对该文件的访问被拒绝时的行为。

有谁知道在测试沙箱中模拟它的简单好方法吗?


  • 我怀疑这可以使用 DirectorySecurity, 来模拟但是,我不确定是否可以在所有情况下都正确地做到这一点:

    Assume that, for example, I can strip access rules for the current user, which is running the tests (and this would also require UAC / Elevation).

    I guess in this case I'd lose the ability to restore these rights and dispose my sandbox correctly after the test is finished (without diving into stuff like impersonation and access token manipulations).

  • 我可能可以用 Moles 做到这一点或任何其他基于模拟的方法,但我对通用解决方案更感兴趣(例如,为 native 应用程序重用此测试)。

我错过了什么?有什么简单方法可以做到这一点?

最佳答案

您没有明确表示要通过安全设置实现此目的,那么为什么不在其他地方获得对该文件的独占访问权呢?

using(File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
//hold here to keep file locked
}

关于c# - 在 C#/Windows 中为文件模拟 'access denied',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14773419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com