有没有办法从锁定的文件中读取数据? [英] Is there any way to read data from a locked file?

查看:179
本文介绍了有没有办法从锁定的文件中读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我心目中:

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

        using (StreamReader rdr = new StreamReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read)))
        {
            rdr.ReadToEnd();
        }
        var t = File.ReadAllBytes(file);



Neigther File.Open(文件,FileMode.Open,FileAccess.Read, FileShare.Read)也不 File.ReadAllBytes 可以读取文件中的数据。

Neigther File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read) nor File.ReadAllBytes can read file data.

从我旧的C ++和WINAPI天,我记得,有曾经是一个很好的方式来读取锁定的文件,如果你有备份权限的,但我不知道如何获取并使用那些在c#。

From my old c++ and winapi days, I do remember, that there used to be a good way to read locked files, if you have backup priviledges, but I have no idea how obtain and use those in c#.

谁能为我提供它已被锁定后如何读取文件的样本?

Can anyone provide me with a sample on how to read a file after it has been locked?

推荐答案

那么,如果该文件被完全锁定(没有的共享),您将无法读取它。如果文件被打开了份额阅读,您将能够使用非侵入性方法来读取:

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
    }
}

这篇关于有没有办法从锁定的文件中读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆