Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
This might seem like a dumb question to those of you that the answer but please give any solutions and please do not down-vote it.

So lets say I'm building a windows form app and I had a Text-box named txt1 and a Button named btnSave. I want to make a program, that when you click btnSave, txt1 will save and even if you close the form and open it back up the same input in txt1 will be there. There is probably a simple way to do this but I have no idea. Any ideas?
Posted
Comments
Sergey Alexandrovich Kryukov 24-Oct-12 22:16pm    
What did you try? If does not require any special knowledge, just some logic.
--SA

You should have found it by yourself, and would not answer if I did not see two not quite adequate "solutions".

Basically, you better use System.IO.StreamWriter. Something like
C#
static void SaveText(string fileName, string value) {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName, false)) { //false means create new
        writer.Write(value); // this way, it could write a number of strings one by one
    } // this automatically calls writer.Dispose
    // that's why it's important to use "using" statement, based on System.IDisposable implemented by writer
    // otherwise file buffer is left not closed which may cause lost data uncommitted to file system
}

//...

SaveText(MyFileName, MyTextBox.Text);


Please see:
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^],
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^],
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[^].

—SA
 
Share this answer
 
Comments
Curtdawg123 25-Oct-12 6:32am    
I did not no what to search for
Sergey Alexandrovich Kryukov 25-Oct-12 13:04pm    
Could you re-phrase it in a correct way? Otherwise, it's not clear what you want to say.
--SA
Espen Harlinn 25-Oct-12 16:12pm    
5'ed!
Sergey Alexandrovich Kryukov 25-Oct-12 16:25pm    
Thank you, Espen.
--SA
Curtdawg123 25-Oct-12 17:24pm    
I didn't now what to enter in the search engine
The best way would be putting the stuff you want into an object, and serialize it, then save: http://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file[^]. Link also has examples of how you can restore.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Oct-12 22:17pm    
There is nothing much to serialize, because OP said this is just text. I guess the question was way more trivial than you might think... (I did not vote this time.)
--SA
Clifford Nelson 24-Oct-12 22:53pm    
Thanks for the comment. It is just text, but still seems that could be quite a few fields, and especially since the link provides a generic to solve the problem, it becomes pretty trivial to save an object.
There are many ways to save your data offline, here are some ways I would like to suggest you:
1. Save data in a database (Using Database engine)
2. Save data in a XML file (Using Xml namespace)
3. Save data in Windows Registry (Using Microsoft.Win32)
4. Save data as Text File (Using StreamWriter ...)
5. Save data as binary file (Using BinaryWriter)
6. Save data as Settings which is supported by .NET (Using namespace Properties)
 
Share this answer
 
Here try this:
C#
if (File.Exists(FullPath))
               {
                   File.Delete(FullPath);
               }
               using (FileStream fs = new FileStream(FullPath, FileMode.Create))
               {
                   BinaryFormatter bf = new BinaryFormatter();
                   bf.Serialize(fs, textbox.Text);
                   fs.Close();
               }


To Load it into a TextBox:
C#
using (FileStream fs = new FileStream(FullPath, FileMode.OpenOrCreate))
               {
                   BinaryFormatter bf = new BinaryFormatter();
                   textBox.Text = (String)bf.Deserialize(fs);
                   fs.Close();
               }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900