Can u help? I need to save data from label to file on hard drive
-
Im new to c#. Can you advice me how to code saving text from labels or message boxes to file on hdd?:wtf:
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.
-
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.
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
-
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
-
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.
-
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.
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 } } }
-
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 } } }
-
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 -
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 -
No. Only if you research each writable stream type. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
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 } } }
-
Thanx for this, I played around with it and it works. What about if I want to load it back to my form?
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[^]