w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
If streamreader doesn't lock a file just to read it, why is the file locked until a dialogue box is clicked away?

I think you need to use a FileStream to do that.

Also, you're not closing your stream or calling Dispose() on it. You should use a using statement to make sure it happens. The following code should ensure the file isn't locked.

private void read1()
{
    using (var fs = new FileStream(@"T:	estfile", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)) {
        using (var tr = new StreamReader(fs)) {
            string input = null;
            while ((input = tr.ReadLine()) != null)
            {
                if (input.Contains("test"))
                {
                    MessageBox.Show(input);
                }
            }
        }
    }    
}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.