Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Streamwriter only writes one item from textbox

Streamwriter only writes one item from textbox

Scheduled Pinned Locked Moved C#
question
9 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    auting82
    wrote on last edited by
    #1

    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);
                    
                }
                   
            }
    
    L 2 Replies Last reply
    0
    • A auting82

      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);
                      
                  }
                     
              }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 use FileMode.Append as described at FileMode Enumeration (System.IO)[^].

      A 1 Reply Last reply
      0
      • A auting82

        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);
                        
                    }
                       
                }
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • L Lost User

          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 use FileMode.Append as described at FileMode Enumeration (System.IO)[^].

          A Offline
          A Offline
          auting82
          wrote on last edited by
          #4

          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);
                          
                      }
                         
          
                  }
          
          L 1 Reply Last reply
          0
          • A auting82

            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);
                            
                        }
                           
            
                    }
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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 use FileMode.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.

            1 Reply Last reply
            0
            • L Lost User

              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

              A Offline
              A Offline
              auting82
              wrote on last edited by
              #6

              Itried that but it didnt work :(

              L 1 Reply Last reply
              0
              • A auting82

                Itried that but it didnt work :(

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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

                A 1 Reply Last reply
                0
                • L Lost User

                  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

                  A Offline
                  A Offline
                  auting82
                  wrote on last edited by
                  #8

                  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);
                  
                                  }
                  
                  
                              }
                  
                  L 1 Reply Last reply
                  0
                  • A auting82

                    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);
                    
                                    }
                    
                    
                                }
                    
                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Been there, done that. One "relaxes" AFTER the problem is solved; not before.

                    "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups