Open file in a unique way c#

Asked

Viewed 52 times

0

Good morning,

I need to open a file . txt in a unique way so that if another program tries to read receva a warning preventing opening.

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

Thank you!

  • Ichihara, I saw that none of your questions you mark as an answer. Take the tour to see how the community works please: https://answall.com/tour

  • 1

    Rovvan, okay I just scored, I’ll see the others.

1 answer

2


Simple, you can use Streamreader. Until you run the Close() method, the file will be blocked 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 the while the variable linha represents each line in the file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.