Home > OS >  Using StreamWriter and StreamReader in a Windows Application
Using StreamWriter and StreamReader in a Windows Application

Time:04-21

I could use some help as I can't seem to work this out but I'm creating a c# windows application and I need to read and write from files. I've mostly got it working as I want but when I write to the file it adds this extra text "System.Windows.Forms.RichTextBox, Text:" and then displays what I want it to.

Is there a way to just input and display the text I'm actually writing rather than adding the additional properties of the text box.

Any help would be massively appreciated.

My code for adding text is

        {
            using (StreamWriter sw = new StreamWriter(@"C:\Users\bin\Debug\net6.0-windows\ManagerStock.txt"))
            {
                sw.WriteLine(managerAddtxt);
            }
        }

and my code for reading is

        {
            using (StreamReader re = new StreamReader(@"CC:\Users\bin\Debug\net6.0-windows\ManagerStock.txt"))
            {
                while (!re.EndOfStream) 
                {
                    managerStocktxt.AppendText(re.ReadLine());
                }
            }
        }

CodePudding user response:

You're getting that because you are writing an object to the stream.

Change your code to the following and you will get what you expect:

sw.WriteLine(managerAddtxt.Text);
  •  Tags:  
  • c#
  • Related