Results 1 to 7 of 7

[C#] - How to store information...

This is a discussion on [C#] - How to store information... within the Source Code Samples forums, part of the Programming category; Er..Well, I need to make a program that allows you to save the info that you enter, than when you ...

  1. #1
    col2eight is offline Member
    Join Date
    Jul 2009
    Posts
    48

    Default [C#] - How to store information...

    Er..Well, I need to make a program that allows you to save the info that you enter, than when you open the program again, it's still there. That's it. So it's basically like a good-looking notepad lmao. Even if it's just a source code, I'll build around it, learning as I go.
    Halp!

  2.   Alt These ads help cover server costs and such.


  3. #2
    Dominus Electus is offline Bot Maker
    Join Date
    Sep 2009
    Posts
    94

    Default

    i have a TextFile class you can use

    go here and ask me for it:
    widget.mibbit.com/?server=darkdna.net%3A6666&channel=%23Dominus

  4. #3
    7usabball is offline Secret Undercover *****
    Join Date
    Jan 2009
    Posts
    1,462

    Default

    Do something like

    If textfile.txt exists Then (stuff here which will do w/e with said stored info) Else (stuff here which will allow writing stuff to said text file)

    just so u no, i have never used C#, so I that line of code obvs. will not work, but i'm giving the directions on how it should work
    ▀█ █▄█ ▄█▀ ▐▀▌


  5. #4
    Mikah is offline Junior Member
    Join Date
    Jun 2009
    Posts
    50

    Default

    You need to look up the system.io namespace and look into streamreader and streamwriter.



    Assuming your building a window forms application, you are going to need to create a new instance of the streamreader class.. then read the information from the text box and write it to the file or vice versa.

    string line;

    Streamreader sr = new Streamreader(filename);

    line = sr.Readline();

    while ( line != null){

    txtBox.appendtext(line) + "/n" <-- new line

    line = sr.Readline() <-- continues reading lines until the value is null which then will exit the while loop.

    }

    sr.Close();



    Have to excuse me thats not exact but a basic outline of what needs to be done to read a text file, you use the streamwriter to write to the file, if you still need help when im not at work or havent figured it out yet ill probably check back.

    You have to make sure to close the streamreaders/writers after your done using them or else they wont save/write properly. Put the streamreader in the forms load event and make a button or use a menu bar to put a save method in.

  6. #5
    999
    999 is offline Junior Member
    Join Date
    Apr 2008
    Posts
    9

    Default

    I think you are looking for ApplicationSettings. It will store the default value and retain the modifications when saved.



    Add "Properties.Settings.Default.Save();" to the form closing event and it should save the information to user.config, so it is available for next use. I'm happy Fatalrus told me about that, much more efficient than a StreamReader.

  7. #6
    Mikah is offline Junior Member
    Join Date
    Jun 2009
    Posts
    50

    Default

    Yea i suppose that could work to, i just wrote a very simple notepad program myself using streamreader/writer took me about two minutes to do

    on the button /filemenus save click event

    Code:
        
                string line;
                StreamWriter sw = new StreamWriter("Text.txt");
    
                line = txtBox.Text;
    
                sw.Write(line);
    
                sw.Close();

    on the load buttons click event

    Code:
                string line;
                StreamReader sr = new StreamReader("Text.txt");
    
                line = sr.ReadToEnd();
    
                txtBox.Text = line;
    
                sr.Close();

    Of course if you wanted to get fancy you could go ahead and use the file saver dialogs and what not to choose the file name location etc but for saving something to reload next time around that will do the trick.
    Last edited by Mikah; 12-17-2009 at 09:41 PM.

  8. #7
    col2eight is offline Member
    Join Date
    Jul 2009
    Posts
    48

    Default

    Hey guys sorry I haven't been on TC in a while. Thanks for all the help. I got it.
    Halp!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Facebook Bots | Admin Forum