锁定的行为是通过网络共享的不同不同、行为、网络

2023-09-03 17:25:50 作者:春风不度玉门关

我一直在试图锁定一个文件,以便其他克隆服务无法访问文件。然后,我读取该文件,并在完成后移动文件。此举被允许使用 FileShare.Delete

I have been trying to lock a file so that other cloned services cannot access the file. I then read the file, and then move the file when finished. The Move is allowed by using FileShare.Delete.

然而,在以后的测试中,我们发现,这种方法是行不通的,如果我们现在看到的是网络共享。我AP preciate我的方法可能不是最好的,但我的具体问题是:

However in later testing, we found that this approach does not work if we are looking at a network share. I appreciate my approach may not have been the best, but my specific question is:

为什么对本地文件下面演示的工作,但不是针对网络文件?

更具体的你可以更好的,因为我发现在我的搜索信息非常少,表明网络股表现不同本地磁盘。

The more specific you can be the better, as I've found very little information in my searches that indicates network shares behave differently to local disks.

string sourceFile = @"C:\TestFile.txt";
string localPath = @"C:\MyLocalFolder\TestFile.txt";
string networkPath = @"\\MyMachine\MyNetworkFolder\TestFile.txt";

File.WriteAllText(sourceFile, "Test data");

if (!File.Exists(localPath))
    File.Copy(sourceFile, localPath);

foreach (string path in new string[] { localPath, networkPath })
{
    using (FileStream fsLock = File.Open(path, FileMode.Open, FileAccess.ReadWrite, (FileShare.Read | FileShare.Delete)))
    {
        string target = path + ".out";
        File.Move(path, target); //This is the point of failure, when working with networkPath

        if (File.Exists(target))
            File.Delete(target);
    }

    if (!File.Exists(path))
        File.Copy(sourceFile, path);
}

编辑:值得一提的是,如果你想从一个网络共享文件移动到另外一个网络共享而锁定到位,这部作品。这个问题似乎只在移动文件时出现的一样的文件共享,而被锁定。

It's worth mentioning that if you wish to move the file from one network share, to another network share while the lock is in place, this works. The problem only seems to occur when moving a file within the same file share while it is locked.

推荐答案

我相信System.IO.File.Open()映射到的Win32 API函数CreateFile()。在这个功能微软的文档[的http:// MSDN .microsoft.com / EN-US /库/ aa363858(V = vs.85)的.aspx ],它提到了以下内容:

I believe System.IO.File.Open() maps to the Win32 API function CreateFile(). In Microsoft's documentation for this function [ http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx ], it mentions the following:

在Windows Server 2003和Windows XP / 2000:共享冲突发生时,如果试图在远程计算机上打开一个文件或目录的删除时dwDesiredAccess参数的值是删除访问标志(0x00010000在)OR'编与任何其他访问标志,以及远程文件或目录尚未打开FILE_SHARE_DELETE。为了避免共享冲突在这种情况下,打开远程文件或目录的删除访问权只,​​或致电的DeleteFile无需先打开文件或目录进行删除。

Windows Server 2003 and Windows XP/2000: A sharing violation occurs if an attempt is made to open a file or directory for deletion on a remote computer when the value of the dwDesiredAccess parameter is the DELETE access flag (0x00010000) OR'ed with any other access flag, and the remote file or directory has not been opened with FILE_SHARE_DELETE. To avoid the sharing violation in this scenario, open the remote file or directory with the DELETE access right only, or call DeleteFile without first opening the file or directory for deletion.

根据这一点,你必须通过删除作为的FileAccess参数IO.File.Open()。不幸的是,在DELETE枚举不包括作为一种选择。

According to this, you would have to pass DELETE as the FileAccess parameter to IO.File.Open(). Unfortunately, the DELETE enumeration was not included as an option.

此问题只适用于Windows 2003和更早版本。我测试在Windows 2008 R2 SP1的code,并能正常工作。因此,它是可能的,它也将在Windows 2008上正常工作。

This problem only pertains to Windows 2003 and earlier. I have tested your code on Windows 2008 R2 SP1, and it works fine. So it is possible that it would also work on Windows 2008 as well.