1

I have this TextWriter

    public static string txt = "./Logs.txt";
    public static TextWriter Logs = File.CreateText(txt);

I want to do something like this

    textBox1.Text = Logs.ToString();

or like this

    Logs.Flush();
    textBox1.Text = "";
    File.WriteAllText(txt, textBox1.Text);

I tried this too

public class ControlWriter : TextWriter
{
    private Control textbox;
    public ControlWriter(Control textbox)
    {
        this.textbox = textbox;
    }

    public override void Write(char value)
    {
        textbox.Text += value;
    }

    public override void Write(string value)
    {
        textbox.Text += value;
    }

    public override Encoding Encoding
    {
        get { return Encoding.ASCII; }
    }
}
//in the Form_Load
    TextWriter TW = new ControlWriter(textBox1);

It works but if the application starts to write continuously, the application will freeze....

Yox
  • 313
  • 2
  • 6
  • 18
  • 2
    So did you try that? Did it work? If not, what didn't work? – mjwills Jun 12 '17 at 00:03
  • Nothing work, the first code output this in the textBox "System.IO.StreamWriter" and the second will give me an error like this "Logs.txt is used by another process" – Yox Jun 12 '17 at 02:51

2 Answers2

1

It seems like you want to read the data, but the question doesn't clearly describes it so I'll assume it.

If so, you can use TextReader ...

For how to use it, see this.

For Documentation

Edit

TextWriter Logs = File.CreateText(txt);
Logs.Close();
TextReader logs = File.OpenText(txt);
String data = logs.ReadToEnd();
logs.Close();

The above code works, you have to close the file which was opened for it to be used in any other process.

However, I would suggest using File to create and read the file. It's easy, you don't have to close the file after creation or reading, and it's short.

Example:

//To create file, where *txt* is your path.
File.WriteAllText(txt, "Whatever you want in file."); 
//To read your file, where *txt* is your path.
String data = File.ReadAllText(txt)
happyNinja1998
  • 50
  • 1
  • 11
  • I don't want to close the `TextWriter` because the logs will stop registering after that... – Yox Jun 12 '17 at 05:28
  • A file can't be opened by two processes simultaneously... The only thing you can do is open the file again after reading it. Did you try my code suggestion? – happyNinja1998 Jun 12 '17 at 10:52
  • In your first code you close the `TextWriter` (I can't reopen it after this) and the second code, you are using a `string` to fill the txt file but I can't replace my `TextWriter` by a `string` – Yox Jun 12 '17 at 19:20
  • `TextWriter` is only for *writing* text, it can't read the text. For reading you have to use `TextReader`. Have you tried using `File`? Maybe that'll work... As for opening the `TextWriter` again, use `Logs = new TextWriter(yourpath);` If it still doesn't work, please give more details about what are you trying to achieve. – happyNinja1998 Jun 12 '17 at 22:36
  • Bro, I know what the `TextWriter` is used for, and no I can't use again `Logs = new TextWriter(txt);` what I want is to output the same things (written into the txt file) from my `TextWriter` into the textBox – Yox Jun 13 '17 at 03:48
1

I found the solution, here is the code I used

            Logs.Flush();
            using (FileStream Steam = File.Open(txt, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (StreamReader Reader = new StreamReader(Steam))
                {
                    while (!Reader.EndOfStream)
                    {
                        textBox1.Text = Reader.ReadToEnd();
                        break;
                    }
                }
            }
Yox
  • 313
  • 2
  • 6
  • 18