Is there any way to read data from a locked file?

后端 未结 2 543

Here\'s what I have in mind:

        var file = @\"myfile\";
        File.Open(file,
                  FileMode.Open, FileAccess.ReadWrite, FileShare.None);
         


        
2条回答
  •  时光说笑
    2021-01-19 06:10

    Well, if the file is totally locked (no sharing) you will not be able to read it. If the file was opened to share read, you will be able to read using a non intrusive method:

    string fileName = @"myfile";
    using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    using (StreamReader fileReader = new StreamReader(fileStream ))
    {
        while (!fileReader .EndOfStream)
        {
            string line = fileReader .ReadLine();
            // Your code here
        }
    }
    

提交回复
热议问题