-3

I wrote an application that search for a specific text in flat files. But the problem that i'm having is that my application end up reading non flat files like doc,docx etc. So i want to prevent this from happening. Is there a logic that i can use to determine if a file is flat file or not ?

By flat files i'm referring to files such as .log, .txt, .config, .cs, .vb etc.

Here is my code to read files :

string[] extension = txtExtension.Text.Split(';');

if (extension.Length == 1)
{
    string[] files = Directory.GetFiles(txtDirectory.Text, "*." + txtExtension.Text.Trim(), SearchOption.AllDirectories);
    for (int append = 0; append < files.Length; append++)
    {

    }

 }
chosenOne Thabs
  • 1,480
  • 3
  • 21
  • 39
  • 10
    Define "flat". After you provided a formal definition for that you would be able to implement it yourself. – zerkms Dec 11 '16 at 21:33
  • By flat file i mean a file without a hierarch. E.g .txt,.config, .log etc. I dont want to process files like .doc/.docx because they have files within them. E.g xmls withing them. – chosenOne Thabs Dec 12 '16 at 20:02
  • I found a solution on this link : http://stackoverflow.com/questions/910873/how-can-i-determine-if-a-file-is-binary-or-text-in-c – chosenOne Thabs Dec 12 '16 at 20:55

1 Answers1

0

I wrote an application that search for a specific text in flat files.

I guess by "flat file" you mean a "text file". Even this concept is fuzzy, and there is no foolproof way of deciding if a file is textual or binary, only some heuristics. You can restrict to some list of file extensions that you assume correspond to textual files, but even this is platform dependent and not 100% safe.

Community
  • 1
  • 1
leonbloy
  • 73,180
  • 20
  • 142
  • 190