Streamwriter only writes one item from textbox
-
I am trying to create a function in the GUI where I Logg the values from a textbox and then allow the user to save these values as a .csv file anywhere on the computer. I have managed to do that, but when I open the files I only see one item from my textbox written in the text file, Why is that ?
private void Logging()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{sw.WriteLine(textSampling.Text); } }
-
I am trying to create a function in the GUI where I Logg the values from a textbox and then allow the user to save these values as a .csv file anywhere on the computer. I have managed to do that, but when I open the files I only see one item from my textbox written in the text file, Why is that ?
private void Logging()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{sw.WriteLine(textSampling.Text); } }
-
I am trying to create a function in the GUI where I Logg the values from a textbox and then allow the user to save these values as a .csv file anywhere on the computer. I have managed to do that, but when I open the files I only see one item from my textbox written in the text file, Why is that ?
private void Logging()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{sw.WriteLine(textSampling.Text); } }
-
You are using
File.Open(saveFileDialog1.FileName, FileMode.CreateNew)
every time, so the file will only ever contain that last item logged. You need to useFileMode.Append
as described at FileMode Enumeration (System.IO)[^].Hi, how am I suppose to implement it with my code. i have code that is doing almost everything I want, only not printing the contents from the text.Box. The strange thing is, when I use a RichTextBox then everything gets printed into the .csv file I am trying to create. :^) Were you thinking about something like this?
private void Logging()
{SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew | FileMode.Append)) using (StreamWriter sw = new StreamWriter(s)) { sw.WriteLine(textSampling.Text); } }
-
Hi, how am I suppose to implement it with my code. i have code that is doing almost everything I want, only not printing the contents from the text.Box. The strange thing is, when I use a RichTextBox then everything gets printed into the .csv file I am trying to create. :^) Were you thinking about something like this?
private void Logging()
{SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew | FileMode.Append)) using (StreamWriter sw = new StreamWriter(s)) { sw.WriteLine(textSampling.Text); } }
I already explained, that if you use
FileMode.CreateNew
you will be creating a new file every time you try to write a log entry. You only need to use that once, at the beginning of a session perhaps*, and useFileMode.Append
for every subsequent call. * You need to use some sort of file name cycling in your programs so you do not overwrite the previously created files. A common method of doing this is to create files with the date as part of the filename. -
For "simple" logging, all you need is: 1) File.AppendAllText, or 2) File.AppendAllLines
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
What is "it"? If it didn't work, you did "it" wrong.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
Yes you are completely correct . I did something very WRONG. Holy Shit , I was writing from wrong text box the whole time. :sigh: X| My initial code actually works;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName,FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{sw.WriteLine(textSensorValues.Text); } }
-
Yes you are completely correct . I did something very WRONG. Holy Shit , I was writing from wrong text box the whole time. :sigh: X| My initial code actually works;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName,FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{sw.WriteLine(textSensorValues.Text); } }