locked
determine the format of a file, .txt or .rtf, before saving RRS feed

  • Question

  • I would like to be able to create a new document, add formatted text to it, and then just click save.

    How would the program know whether to save it as .txt or .rtf?

    Saturday, July 23, 2011 6:06 PM

Answers

  • If you loaded it as a formatted text file or added formatting, it's a formatted text file.  If you want to save any formatting, save it as a RTF.  If you're not sure, save it as RTF.  Want to check if it's pure text?  Ensure no byte > 127.
    • Proposed as answer by Frank L. Smith Saturday, July 23, 2011 10:33 PM
    • Marked as answer by Kee Poppy Thursday, August 4, 2011 7:17 AM
    Saturday, July 23, 2011 10:31 PM

All replies

  • I would like to be able to create a new document, add formatted text to it, and then just click save.

    How would the program know whether to save it as .txt or .rtf?


    I'm assuming you're using a RichTextBox?

    If so, it's one of the overloads of the "SaveFile" method:

     ' Save as plain text:
        RichTextBox1.SaveFile("c:\test.rtf", RichTextBoxStreamType.PlainText)
    
        ' Save as an RTF file:
        RichTextBox1.SaveFile("c:\test.rtf", RichTextBoxStreamType.RichText)
    
    

     

    Saturday, July 23, 2011 6:14 PM
  • Thank you for your quick reply.

    Yes, I am using a richtextbox.

    What I am wondering, is there a way the program can determine the format of the text and then

    make the decision to saveas PlainText, or Richtext automatically

    Saturday, July 23, 2011 7:51 PM
  • What I am wondering, is there a way the program can determine the format of the text and then

    make the decision to saveas PlainText, or Richtext automatically


    Oh I see what you're saying here now.

    Hmm - let me think on that one a while!

    Saturday, July 23, 2011 7:55 PM
  • Well I tried something that didn't work. Obviously one cannot prove a negative; just because I don't know how doesn't mean that it doesn't exist.

    I'll be curious to see what others might have to say about it.

    Sorry I couldn't help :(

    Saturday, July 23, 2011 8:07 PM
  • What would you say is the difference between plain text and formatted text? Is plain text (.txt) determined by having no changes of format within it? What is the end-use for the file you save?

    --
    Andrew

    Saturday, July 23, 2011 8:28 PM
  • I would say plain text is text with no formatting.

    Say I open a plain text file and I decide I am going to change the font, and maybe the fontcolor and size

    and then hit the save button.

    I want my program to recognize the these things have been changed and that if it is saved as plaintext I will lose all formatting.

    Like what wordpad does now.

    Saturday, July 23, 2011 8:45 PM
  • I'll explain what I tried - for the sake of saying that the obvious didn't work.

    I thought that what I'd do would be to create a second RTB (eventually I'd make it invisible but I never got that far), then in the button's click event (to save the file) I would first copy the TEXT of RTB1 to RTB2. Once done, now compare the .Rtb of both. My theory being that if they were both the same, it was unformatted text.

     

     

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        If RichTextBox1.TextLength > 0 Then
          RichTextBox2.Text = RichTextBox1.Text
    
          If RichTextBox1.Rtf = RichTextBox2.Rtf Then
            Stop
          Else
            Stop
          End If
        End If
    
      End Sub
    


    Nope - they're different. You can put a breakpoint in and look and indeed they're not the same.

    Why not ... I have no idea.

    Saturday, July 23, 2011 9:16 PM
  • If  you want to save anything other than the ascii values of the text, save the contents as RTF.  You may have tried to save a document as text in Word or Wordpad and  received a warning to the effect that all formatting will be removed.  That's what happens when you save the text in a RichTextBox as text; all formatting is removed.
    Saturday, July 23, 2011 9:53 PM
  • If  you want to save anything other than the ascii values of the text, save the contents as RTF.  You may have tried to save a document as text in Word or Wordpad and  received a warning to the effect that all formatting will be removed.  That's what happens when you save the text in a RichTextBox as text; all formatting is removed.


    John,

    True enough, but why can it not be detected that it's formatted text?

    Even if my little experiment had worked, it sure seems that there should be a built-in method like ".IsFormatted" to do this ... do you disagree?

    Saturday, July 23, 2011 9:59 PM
  • OK, if the only way that the text can be formatted is through your program, then you can set a flag when a method to format the text is called. When you save the data, use that flag to determine whether to save the .PlainText or .RichText version.

    However, if the user could paste richtext in, you might need to investigate parsing the .RichText to see if there are any changes of format within it.

    --
    Andrew

    Saturday, July 23, 2011 10:31 PM
  • If you loaded it as a formatted text file or added formatting, it's a formatted text file.  If you want to save any formatting, save it as a RTF.  If you're not sure, save it as RTF.  Want to check if it's pure text?  Ensure no byte > 127.
    • Proposed as answer by Frank L. Smith Saturday, July 23, 2011 10:33 PM
    • Marked as answer by Kee Poppy Thursday, August 4, 2011 7:17 AM
    Saturday, July 23, 2011 10:31 PM
  • If you loaded it as a formatted text file or added formatting, it's a formatted text file.  If you want to save any formatting, save it as a RTF.  If you're not sure, save it as RTF.  Want to check if it's pure text?  Ensure no byte > 127.

    Brilliant! :-D
    Saturday, July 23, 2011 10:33 PM
  • Are you sure?  I'm not sure it's possible to have pure text in a RichTextBox.  That is, the contents of a RichTextBox is RTF even if no formatting has been applied.
    Saturday, July 23, 2011 10:38 PM
  • Are you sure?  I'm not sure it's possible to have pure text in a RichTextBox.  That is, the contents of a RichTextBox is RTF even if no formatting has been applied.


    You're right. That now makes perfect sense.

    Whether the formatting has changed via the user's program or was pasted in from another source, simply being IN the RTB now makes it "RichText".

    Interesting observation, and you're spot on.

    Hmm ...

    Saturday, July 23, 2011 11:03 PM
  • OK, if the only way that the text can be formatted is through your program, then you can set a flag when a method to format the text is called. When you save the data, use that flag to determine whether to save the .PlainText or .RichText version.

    However, if the user could paste richtext in, you might need to investigate parsing the .RichText to see if there are any changes of format within it.

    --
    Andrew


    Andrew,

    I wasn't overlooking your point - it's a good one that I agree with (although I really go back to what both you and John were alluding to - always save it as a .rtf file and be done with it).

    My reason for not commenting was that in my mind, the question really was "How Can I Detect If It's Formatted Text".

    I hope you agree. :)

    Saturday, July 23, 2011 11:05 PM
  • You all have come upon yet another weakness of Windows and the Widows files system NTFS. Windows, god forbid, uses file extensions to identify characteristics of a file and not file attibutes.

    Other file systems store much more information about a file and are characteristically slower which makes little difference these days.

    Renee


    Sunday, July 24, 2011 4:02 AM
  • Sometime the format of a text file may be identified by a bom mark in the first 3 bites of a file. You may be able to use that and the fille extention.

    Renee

    Sunday, July 24, 2011 5:11 AM
  • The is something that may help. If the file is stream read

    Tasksearch

    .enc = sr.CurrentEncoding.HeaderName

    The streamreader has a bit called Currentencoding.

    Renee

     

    Sunday, July 24, 2011 5:31 AM
  • It is not the wind who decide that, but the one who uses your program if you have given them the possibilities.

    So let the user decide and give him those possibilities and create code for both.

    There are many ways to do that, from a menu option, button bar, savefiledialgo to a checkbox which you have set on your screen (but many more)

    The used text is not important in this.

     


    Success
    Cor
    Sunday, July 24, 2011 5:54 AM