Opening a file's Shadow Copy if the current copy is in use

后端 未结 3 1667
天命终不由人
天命终不由人 2021-01-12 09:04

I\'m trying to backup files on a server, but some of them are in use and cannot be opened. Instead, I\'d like to open their shadow copy if the current copy is in use. How ca

3条回答
  •  情歌与酒
    2021-01-12 09:33

    If you have control of the first process you can specify file handle share type

    string contents1;
    string contents2;
    using (FileStream fs1 = new FileStream("test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using (var tr1 = new StreamReader(fs1))
        {
            using (FileStream fs2 = new FileStream("test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var tr2 = new StreamReader(fs2))
                {
                    contents2 = tr2.ReadToEnd();
                    contents1 = tr1.ReadToEnd();
                }
            }
        }
    }
    
    Console.WriteLine(contents1);
    Console.WriteLine(contents2);
    

提交回复
热议问题