3

To read the following:

Control Panel->Region and Settings->Formats->Additional Settings->Numbers-> List Separator

I used:

System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator in winform/wpf application. It gives me the correct list separator. which I can use for CSV file operations.

But the same separator I don't get with the same line of code in window service. It gives the default separator based on culture.

How to get the correct list separator?

Excel uses the same separator(in control panel-region settings) when we save a file in csv format.

Thanks in advance.

Pankaj_Sen
  • 61
  • 9

1 Answers1

0

The behavior you noticed is the correct one. That is because the regional settings are per user. Meaning that each user can use its own regional settings independent from the rest of the users. After all, it's a matter of preference. That makes sense.

However, most likely, your windows service runs as Local System (or some other service account) which, in turn, has its own settings. Windows makes it easy to change the local system regional settings by providing the option to copy current user settings to the system accounts.

enter image description here

EDIT: You have to look up the currently logged in user first. See GetCurrentUsername() method in the following class from HERE. The code was not entirely written by me, I've picked it up myself some time ago and also brought some modifications to it, among which is the GetCurrentUsername() method.

Further on, once you get the username, you get proceed with finding the user's SID, as done in the following question's accepted answer: Convert a username to a SID string in C#/.NET

Once you get that also, it's just a matter of reading the list separator from the registry value sList unde the following key:

[HKEY_USERS\SID-YOU-JUST-FOUND\Control Panel\International]
Community
  • 1
  • 1
Mihai Caracostea
  • 8,336
  • 4
  • 27
  • 46
  • I understand the point. But how do I get the "List Separator" for that specific user then in the service? do I need to run the service with 'user' account? – Pankaj_Sen Dec 09 '15 at 11:04
  • @FanOfStackOverflow Five it a try. That's the fastest way of finding out. – Mihai Caracostea Dec 09 '15 at 11:08
  • @FanOfStackOverflow By the way... You are aware that you can set the service CurrentCulture at any time, right? – Mihai Caracostea Dec 09 '15 at 11:10
  • Thanks Mihai Caracostea, Yes I know that I can set it, but then it will be specific right? [Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");]...What I want is to run the service with Local system account and get the "List Operator" according to the logged-in user's regional settings. – Pankaj_Sen Dec 09 '15 at 11:48
  • @FanOfStackOverflow What if there are two different users logged in? – Mihai Caracostea Dec 09 '15 at 11:51
  • The scenario is something like this : an user will login and create a CSV using Excel. Then the separator in CSV will be used according to the logged in user's regional setting in control panel. The service should be capable of reading the same csv file. Then i need to know the correct separator to read the csv. – Pankaj_Sen Dec 09 '15 at 11:59
  • thank you very much Mihai Caracostea for understanding my requirement correctly. It nicely worked for me. big thanks. – Pankaj_Sen Dec 09 '15 at 18:06
  • @FanOfStackOverflow You're welcome! However, keep in mind that it would be far better if you could determine the user who actually created the doc, not just the one who's logged in at the current time. It's just a matter of time until you'll face the situation when active user != document creator. And if they happen to have different list separators... well... you get the point. – Mihai Caracostea Dec 09 '15 at 18:09