Looking for info on filehandling

I am looking for some good info on file IO with visual C#, i'm a log-file maniac and among other things i want to dump all the textboxes into a plain textfile.

I'm also looking for info on how to interact with a printer.

Any info will be helpful, thanks,



Answer this question

Looking for info on filehandling

  • WinstonPennypacker

    .NET has a printer class which you can use and print to the printer. Many examples here on the forums on how to do this.

    http://msdn2.microsoft.com/en-us/library/system.drawing.printing.printdocument.print.aspx

     

    in regards to textfiles, take a look at how to write to a file:

    http://msdn2.microsoft.com/en-us/library/system.io.streamwriter.aspx

    (also scroll down right to the bottom and there are links there on how to write to a file)

    alternatively if you are just logging, you could also log to the System event logs as well by using the EventLog class

    http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx

     

    I hope this gives you a head start!



  • hammar

    Thanks for the reply, i'm still haivng some trouble though.

    In my test application i want to simply save the contents of a rich textbox to a txt file.
    this is the code:

    System.IO.StreamWriter myText = new System.IO.StreamWriter(@"c:\test2.txt");
    myText.Write(mainText.Text);
    myText.Close();

    The problem is that it just saves everything in a single line, im trying to achive a notepad like thing but it just dosent work.
    Do i really have to loop through the string and manually look for the new line characters -_-


  • Xcel

    Thanks that worked perectly.
    What i meant by "one line" is that when i wanted to save

    "hi
    bla

    yoyo"

    the contents of the textfile became:
    "hiblayoyo"

    Just as if it ignored or treated the return signs as text, however i think it's just notepad (wich i used to open and check the files in) that dosen't get it.

    Anyway thanks.


  • bezlali

    well fortunately for you, the RTB control has a SaveFile method. Simply call it, give it the name of the file to save to and the richtextboxstreamtype enum value. In this case, plain text:

     

    this.theRichTextBoxControl.SaveFile("filename.txt", RichTextBoxStreamType.PlainText);

     

    does this work

    the code you posted should work also and should save the contents of the richtextbox to the file...no idea what you mean by "one line". it should save the entire contents as is, just like the code above



  • Looking for info on filehandling