Uniquely open file C #

0

Good morning,

I need to open a .txt file exclusively so that if another program tries to read, it receives a warning preventing it from opening.

The file I'm going to read is a file that contains data in a specific layout and when opening I need to read line by line.

Thank you!

    
asked by anonymous 23.02.2018 / 15:29

1 answer

2

Simple, you can use StreamReader. While not executing the Close () method, the file will be locked by the operating system.

Code:

using (StreamReader rd = new StreamReader("arquivo.txt",Encoding.Default))
{
    string linha = null;
    while ( (linha = rd.ReadLine()) != null)
    {
        //sua linha = linha
    }

    rd.Close();
}

Within% w / w% the variable% w /% represents each line of the file.

    
23.02.2018 / 15:34