检查是否可以写入 FileShare 而无需实际写入

Check that can write to FileShare without actually writing to it

我想要实现的是能够检查我们是否具有对文件共享位置的写入权限,但实际上没有写入任何内容。

我知道检查您是否可以写入某个位置的最简单且可能最可靠的方法就是直接写入。不过这里不合适。

场景是这样的。有一个文件共享,我们的系统生成的文件被写入。在另一端的某个地方,我们的客户正在拾取写入该文件夹的所有内容并将其移动到他们想要的地方。作为系统监控的一部分,我们每隔 10 分钟左右检查一次我们是否有权访问共享。过去,我们通过编写一个空文本文件来完成此操作。但是现在他们正在使用这个自动化系统将文件移出,但这是不合适的。

所以我想知道是否有一种方法可以检查我是否可以在不实际写入的情况下写入某个位置。

我们正在尝试访问的共享是一个网络位置,需要凭据才能访问它。

我试过使用 FileIOPermission 但这表示我有权限,即使我无法访问共享(因为我没有登录)所以当我尝试实际写入它时,我得到一个例外。

private bool hasWriteAccessToFolder(string folderPath)
{
    try
    {
        // Attempt to get a list of security permissions from the folder. 
        // This will raise an exception if the path is read only or do not have access to view the permissions. 
        System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath);
        return true;
    }
    catch (UnauthorizedAccessException)
    {
        return false;
    }
}

借自:C# Test if user has write access to a folder

在这种情况下,我假设 FileShare 的行为类似于典型的 Windows 目录。 据我所知 - Windows 在后台完成所有工作,将网络共享视为目录,并且它应该使用相同的权限系统等。