0

I have a system that processes TXT files and RTF files and converts them using a 3rd party tool. I have to integrate with another system that produces files with a .RTF extension but are actually just .TXT files. Unfortunately there is no consistent file names or text to search for. I have no control over the source files.

Is there a way in c# to check if a file has an RTF file header so I can establish if the file is RTF or TXT without using the extension?

This is part of a web application so I dont want to automate opening MS word or any other bulky tools to check if a file is RTF or not.

Jammy
  • 779
  • 2
  • 13
  • 26
  • 1
    A txt file has no content requirements. Literally any file is a valid "txt" file. – gunr2171 May 20 '22 at 15:20
  • 1
    The RTF magic number is `{\rtf1`. You could try checking for that? – Julia May 20 '22 at 15:22
  • https://docs.fileformat.com/word-processing/rtf/ states that rtf file start with a header `\rtf1\fbidis` – 83lynx May 20 '22 at 15:23
  • tnx, I also Just found this, trying it out. https://stackoverflow.com/questions/22502924/how-to-determine-text-format-in-c-sharp – Jammy May 20 '22 at 15:23
  • @Julia well, [sometimes](https://stackoverflow.com/questions/22502924/how-to-determine-text-format-in-c-sharp#comment112021001_22503136) – gunr2171 May 20 '22 at 15:23

0 Answers0