我一直在试图锁定文件,这样其他克隆服务无法访问文件不同。然后我读取文件,然后在完成后移动文件。移动是通过使用FileShare.Delete
来允许的。锁定行为是通过网络共享
然而在以后的测试中,我们发现,如果我们看一个网络共享这种方法是行不通的。我很感激我的方法可能不是最好的,但我的具体问题是:
为什么对本地文件下面的演示工作,但不是针对网络文件?
更具体的你可以更好的,因为我发现在我搜索的信息非常少,表示网络股表现不同本地磁盘。
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);
}
编辑:值得一提的是,如果你想将文件从一个网络共享移动到另一个网络共享而锁定到位,这个工程。该问题似乎只发生在文件共享同时锁定时移动文件。
检查您是否有权限的共享文件夹的写。 – Nayan 2010-04-23 10:20:09
权限已被检查,并没有问题。活跃用户对目录和共享拥有完全控制权共享 – MattH 2010-04-23 10:41:42
什么样的操作系统提供共享? – 2010-04-23 11:51:46