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. Can u help? I need to save data from label to file on hard drive

Can u help? I need to save data from label to file on hard drive

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
13 Posts 5 Posters 0 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.
  • E evrastil

    Im new to c#. Can you advice me how to code saving text from labels or message boxes to file on hdd?:wtf:

    X Offline
    X Offline
    XRaheemX
    wrote on last edited by
    #2

    There are many many different ways to do this. I would research the System.IO.File and System.IO.TextReader and System.IO.TextWriter classes for more help. I could give you an example, but I don't think it would do any good unless we knew what exactly you're trying to do. It could be good as well to research Serialization as this is a nice easy way to Serialize and object to a file.

    D E 2 Replies Last reply
    0
    • X XRaheemX

      There are many many different ways to do this. I would research the System.IO.File and System.IO.TextReader and System.IO.TextWriter classes for more help. I could give you an example, but I don't think it would do any good unless we knew what exactly you're trying to do. It could be good as well to research Serialization as this is a nice easy way to Serialize and object to a file.

      D Offline
      D Offline
      David Stone
      wrote on last edited by
      #3

      Bryan, what do you do, sit here and refresh the C# forum every three seconds? ;P You're not trying to beat Nish and CG's post counts, are you? ;)


      Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson

      X 1 Reply Last reply
      0
      • D David Stone

        Bryan, what do you do, sit here and refresh the C# forum every three seconds? ;P You're not trying to beat Nish and CG's post counts, are you? ;)


        Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson

        X Offline
        X Offline
        XRaheemX
        wrote on last edited by
        #4

        Haha, I like to take time during my breaks an help people out ;) I'll catch up to them some day... when I'm older and wiser ;)

        1 Reply Last reply
        0
        • X XRaheemX

          There are many many different ways to do this. I would research the System.IO.File and System.IO.TextReader and System.IO.TextWriter classes for more help. I could give you an example, but I don't think it would do any good unless we knew what exactly you're trying to do. It could be good as well to research Serialization as this is a nice easy way to Serialize and object to a file.

          E Offline
          E Offline
          evrastil
          wrote on last edited by
          #5

          Could you give me a simple example please? What I want to do is simply to save text to something.txt file from textbox or label on button_click event.

          X 1 Reply Last reply
          0
          • E evrastil

            Could you give me a simple example please? What I want to do is simply to save text to something.txt file from textbox or label on button_click event.

            X Offline
            X Offline
            XRaheemX
            wrote on last edited by
            #6

            ok.. a VERY simple example would be as such: (This is not at all the complete or best way to do it, just a simple example to start your building blocks)

            public void WriteFile(string text, string fileName)
            {
                System.IO.TextWriter tw = null;
                try
                {
                   if(System.IO.File.Exists(fileName))
                   {
            	  System.IO.File.Delete(fileName); //Do this if you want to "overwrite" the file
                   }
                   tw = System.IO.File.CreateText(fileName);
                   tw.WriteLine(text);
                }
                finally
                {
                   if(tw != null)
                   {
                       tw.Flush();
            	   tw.Close();  // Put these in the finally clause to make sure that the file closes
                   }
                }
            }
            
            D E 2 Replies Last reply
            0
            • X XRaheemX

              ok.. a VERY simple example would be as such: (This is not at all the complete or best way to do it, just a simple example to start your building blocks)

              public void WriteFile(string text, string fileName)
              {
                  System.IO.TextWriter tw = null;
                  try
                  {
                     if(System.IO.File.Exists(fileName))
                     {
              	  System.IO.File.Delete(fileName); //Do this if you want to "overwrite" the file
                     }
                     tw = System.IO.File.CreateText(fileName);
                     tw.WriteLine(text);
                  }
                  finally
                  {
                     if(tw != null)
                     {
                         tw.Flush();
              	   tw.Close();  // Put these in the finally clause to make sure that the file closes
                     }
                  }
              }
              
              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #7

              Doesn't Close() call Flush() automatically?

              D X 2 Replies Last reply
              0
              • D Dan Neely

                Doesn't Close() call Flush() automatically?

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #8

                That depends on what you're calling .Close() on. I just find it easier to read, and less "implied", to call it myself, no matter what stream type is in use. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                D 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  That depends on what you're calling .Close() on. I just find it easier to read, and less "implied", to call it myself, no matter what stream type is in use. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  D Offline
                  D Offline
                  Dan Neely
                  wrote on last edited by
                  #9

                  IS there a list of which streams do/don't call it then?

                  D 1 Reply Last reply
                  0
                  • D Dan Neely

                    IS there a list of which streams do/don't call it then?

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #10

                    No. Only if you research each writable stream type. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    1 Reply Last reply
                    0
                    • D Dan Neely

                      Doesn't Close() call Flush() automatically?

                      X Offline
                      X Offline
                      XRaheemX
                      wrote on last edited by
                      #11

                      I think it does currently but you can never tell what they might change in future releases. So I thought it'd be safe to go ahead and call it anyway.

                      1 Reply Last reply
                      0
                      • X XRaheemX

                        ok.. a VERY simple example would be as such: (This is not at all the complete or best way to do it, just a simple example to start your building blocks)

                        public void WriteFile(string text, string fileName)
                        {
                            System.IO.TextWriter tw = null;
                            try
                            {
                               if(System.IO.File.Exists(fileName))
                               {
                        	  System.IO.File.Delete(fileName); //Do this if you want to "overwrite" the file
                               }
                               tw = System.IO.File.CreateText(fileName);
                               tw.WriteLine(text);
                            }
                            finally
                            {
                               if(tw != null)
                               {
                                   tw.Flush();
                        	   tw.Close();  // Put these in the finally clause to make sure that the file closes
                               }
                            }
                        }
                        
                        E Offline
                        E Offline
                        evrastil
                        wrote on last edited by
                        #12

                        Thanx for this, I played around with it and it works. What about if I want to load it back to my form?

                        X 1 Reply Last reply
                        0
                        • E evrastil

                          Thanx for this, I played around with it and it works. What about if I want to load it back to my form?

                          X Offline
                          X Offline
                          XRaheemX
                          wrote on last edited by
                          #13

                          Well, for the simple one line of text in a file you won't need to parse anything. You can simply do just the opposite of what I have above. Check if the file exists but don't delete it. Instead of using File.CreateText you will use File.OpenText. Instead of using System.IO.TextWriter use a System.IO.TextReader. Instead of using WriteLine() use ReadLine(). It's all pretty simple. However, if you're putting multiple lines in the file for multiple objects/properties it does get a little more complex. A more simple way to do this would be to just serialize the label (or whatever other oject you have) into a file and deserialize it back from the file to an object in the code whenever you need it. A good example exists right here on code project: http://www.codeproject.com/csharp/objserial.asp[^]

                          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