How do I detect the user's locale to get the correct csv separator?
Use:
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator
Writing CSV: The "List separator" string should be used as the delimiters in CSV (see below on how to change this variable). Changing the value of the "List separator" is also reflected in Excel when saving as CSV.
Reading CSV: Determining the delimiter in CSV is another story and it is a bit more complex. In principle it is possible to use a "," as a CSV delimiter in one system and use a ";" or even a "*" or any ("string") as a delimiter on another system: This article provides some insights on how to detect CSV delimiters where reading cross-systems CSV files:
http://www.codeproject.com/Articles/231582/Auto-detect-CSV-separator.
Also you can perform some tests on your exporter by changing the "List separator" value in Windows as follows (might differ between with each Windows OS):
- Open Region and Language dialog.
- Select on the "Format" tab.
- Click on "Additional Settings"
- Edit the value of the "List separator"
As others have mentioned CSV in general should be comma-separated and fields should be double-quoted. However there is also MS Excel specific behavior that causes a correct CSV file to be imported incorrectly. That is because MS Excel by default uses list separator set in Windows System in 'Regional and language options'. For US/UK locale it is comma but for such languages as German it is semicolon. So for MS Excel the option is to use different separator per locale.
The CurrencyDecimalSeparator property contains the decimal separator for the given culture. This being said the CSV separator is not culture dependent. It is a property of the CSV file which you indicate to the parser. And talking about parsers I sincerely hope that you are not rolling your own CSV parser.