Can’t seem to find this anywhere on stackoverflow so here it goes:
I have a file, I want to discover whether it is pipe(|) or comma(,) seperated. I also want to tell whether the text qualifier is a quote(“) or nothing. Anyone have any C# functions that do this? Thanks!
For text-separated files such as this I find the TextFieldParser to be a very useful tool. (You can import the visual basic dll to use it in a C# app).
The general strategy that I would use, since according to you there are a fixed number of columns per file, would be to pick a delimiter and continue parsing/reading lines until one line has a different number of columns than the previous line. When that happens switch to the other delimiter (not sure what you want to do if both are invalid). You may want to also throw out the delimiter if it isn’t found at all on the first line. Using the
TextFieldParserwith HasFieldEnclosedInQuotes set to true you can properly handle fields that are escaped in quotes (it will still work just fine if no quotes are used). This will be much easier than trying to manually handle quotes when using regular string manipulation.