-1

I manage to write code in C++ that works what is needed. Ill paste it down. But its console application and i need windows form application. So i tried to do it in the visual studio 2017 and C#.

I need an application that will post on screen, only last line from .txt file but that .txt file needs to be editable while app is running. So when i add new line in .txt and hit ctrl+s it will automaticly update results in app and it will not show me

"NOTEPAD: The process cannot access the file becouse it is being used by another process."

This is the C++ code that works perfect:

std::ifstream ifs("test.log");

if (ifs.is_open())

{std::string line;

    while (true)

    {while (std::getline(ifs, line)) std::cout << line << "\n";
        if (!ifs.eof()) 
break; 
        ifs.clear();
    }
}
return 0;}

it returns last line from test.log file and when i paste new one in test file it automaticly change after saving the changes in test.txt file.

And here is the code that i write in C# and it works BUT after i run it it wont allow me to save changes in file that i ame making changes.

CODE:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;
using System.IO;


namespace lastone
{
    public partial class last : Form
    {
        public last()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();



                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                    {
                        if ((myStream = openFileDialog1.OpenFile()) != null)
                        {

                            string strfilename = openFileDialog1.FileName;

                        richTextBox1.Text = File.ReadLines(strfilename).Last();

                        }
                    }
                }

        }
    }
}

In this case i load test.txt file with button and openfiledialog... and then just printout last line in richTextbox.

Can anyone give me advice how to make test.txt file editable while my app is working? And eaven if i manage to bypass that problem how can i make the app that i dont need to open file all over again to finde changes in test.txt? i need it to "refresh" automaticly...

P.S. i am literally NEW to programing so dont judge me :) and i am not native eng speaker so sorry for mistakes. I googled so much stuff that i am only confused... TY for any help.

1 Answers1

0

Here's an example console app of how to watch a file for changes with a FileSystemWatcher:

FileSystemWatcher watcher = new FileSystemWatcher();

void Main()
{
    // get file from OFD etc
    watcher.Path = @"c:\temp";
    watcher.Filter = "myfile.txt";
    watcher.Changed += OnChanged;
    watcher.EnableRaisingEvents = true;

    // needed for this example as a console app doesn't have an 
    // event loop unlike Windows Forms
    while (true) {
        Thread.Sleep(100);
    }
}

private static void OnChanged(object source, FileSystemEventArgs e)
{
    string line = null;
    using (FileStream logFileStream = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (StreamReader logFileReader = new StreamReader(logFileStream))
    {

        while (!logFileReader.EndOfStream)
        {
            line = logFileReader.ReadLine();
        }
    }

    // Event fires twice unders some circumstances
    // https://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice
    // so ignore the value if it is empty
    if (!string.IsNullOrEmpty(line))
    {
        // do what you want with the line here
        Console.WriteLine("last line: " + line);
    }

}

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • I have working console app but i need to convert it to windows app... ik that there is not magic button to convert :) i manage to make it in c# to write it in richtextbox when i open the file with opendialogbox on button, but i dk how to add permisions to write to txt file while my app is using it... – lastone Nov 21 '18 at 11:17
  • To convert this to a winforms app, you need add the file system watcher as a field on the form, and in `button1_Click` when you get the filename from the OpenFileDialog 1) get the current last line and 2) start watching the file for changes. Change the line under _"do what you want with the line here"_ to write the line to the RTB. I have tested this code and it works with an open file. – stuartd Nov 21 '18 at 12:02