-2

i have a file.log continually writing , i copy this file into my desktop with some script and i test if the a keyword is on the last lane on the log , if yes i show a green picture if not i show a red picture the probleme that when i start my program i get this error her is the code

    {

        // File.ReadAllLines(@"C:\\Users\\Reta\\Desktop\\TEST\\TEST\\fichiers\\k20\\winvsrTEST.log").Last();
        // System.IO.StreamReader file = new System.IO.StreamReader(@"C:\\Users\\Reta\\Desktop\\TEST\\TEST\\fichiers\\k20\\winvsrTEST.log");
        string motcle1 = "oee code";
        //string line = File.ReadLine().Last().ToString();
        var lines = File.ReadAllLines(@"C:\Users\Reta\Desktop\TEST\TEST\fichiers\k20\winvsrTEST.log");
        string line = lines.Last();


        //line = File.ReadAllLine();
        //do
        {
            if (line.Contains(motcle1))
            {

                pictureBox2.Show();
                pictureBox1.Hide();


            }
            else
            {
                pictureBox2.Hide();
                pictureBox1.Show();
            }


        }
        //while ((line = File.ReadLine()) != null);
        label1.Text = "Hi";

    }
}

}`

Reda HellJoka
  • 45
  • 1
  • 9

1 Answers1

-1

Try following...

FileStream s2 = new FileStream("FileName", FileMode.Open, FileAccess.Read, FileShare.Read);
        StreamReader sr = new StreamReader(s2);

        while (!sr.EndOfStream)
        {
            string line;
            if ((line = sr.ReadLine()) != null)
            {
                //do your work here
            }
        }
Rudra
  • 148
  • 9
  • Try explaining why. – CodeCaster Jan 03 '17 at 11:01
  • @Rudra, when answering questions on Stack Overflow, it's always best to provide a quick description describing what it is exactly that your code does, or why you did it that way, to make it easier for others to read and to understand _why_ your code works. It's also often useful to post a link to the signature definition of a method, i.e. https://msdn.microsoft.com/en-us/library/5h0z48dh(v=vs.110).aspx – XtraSimplicity Jan 03 '17 at 11:03
  • hmm...Because you are trying to open file in read/write mode. you need to open file in read mode. – Rudra Jan 03 '17 at 11:05
  • Find the same question on stackoverflow. http://stackoverflow.com/questions/12744725/how-do-i-perform-file-readalllines-on-a-file-that-is-also-open-in-excel – Rudra Jan 03 '17 at 11:06
  • does file stream alow me to read the log lane per lane and use the last lane ? – Reda HellJoka Jan 03 '17 at 11:33
  • Create streamreader object by passing the filestream object. streamreader has a function called readline(). you can use readline function to read file line by line. – Rudra Jan 03 '17 at 11:39