Note that there are some explanatory texts on larger screens.

plurals
  1. POLocking behaviour is different via network shares
    primarykey
    data
    text
    <p>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 <code>FileShare.Delete</code>.</p> <p>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:</p> <p><strong>Why does the below demo work against the local file, but not against the network file?</strong></p> <p>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.</p> <pre><code>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); } </code></pre> <p>EDIT: 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 <em>the same</em> file share while it is locked.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload