Default user Locale through .Net

kick it on DotNetKicks.com

For who doesn’t know this yet, I’m a software programmer !
Currently I’m working with Visual Studio 2005 and develop software with C#.

And every now and then you get stuck in a situation that seems hopeless :). Even with a large team of capable people and the vast code example base available on the internet, some problems seem unsolvable !
So let me tell you a small story about one of this problems I recently had.

First the current layout of the project, we create software, but we actually sell a complete system to our customers. This means that we will ship a Dell desktop model together the software ! Then our service people will run the install scripts on site and configure it to the needs of the customer. But during the configuration, the service guy has to create several users through the configuration program. These users are stored in Windows XP as actual users, so when the service guys creates a new user, the default settings for this user are coming from Windows XP itself.
More technical detail : there is a .Default user defined in the Windows Registry that acts as a template for each new user !

Now the problem with this setting… Each time the service guy adds a user he has to set this user to the correct Locale, depending on the country where the software is going to get used ! This because the user account for the service people is put to English – US and it could be that each user has to be set to Dutch – Belgium.

To facilitate the install, we had the idea of manipulating Windows XP in such a manner that during configuration of our software the service guy can pick one Locale that will have the effect that each time he adds a new user it will already have those Locale settings.

For this problem I was asked to come up with a good solution in C# !!
And well to cut a long story short, I haven’t found one !!!!! So I was stuck.
The only real solution I found had to many pitfalls, we regarded it as a ‘no go’. It consist of creating a CultureInfo object from a given LCID and dumping all data into the Registry node of HKEY_USERS\.Default\Control Panel\International ! Even though I think this should/could work, I think that string manipulating each value to the correct format would lead to many possible errors.

What happens when I’m stuck… I go around the internet and search for topics that are closely linked. Well let me say that since the dawn of Blogs, we are blessed with information and knowledge that has impact on real life development !!
As a blogger myself, I know that writing a good article with some tutorial knowledge so since developers started using this medium my life got a lot easier :).
Because of my current working position I follow a lot of MSDN blogs that have a close or near relationship with .Net, Visual Studio or C#. One of those blogs is the one of Shawn Steele, and let me tell you his blog is superb ! Easy to follow and the example code/solutions he posts are all eye openers for me.

Well because he had already written many posts about cultural settings, I tried to contact him to see if he could shed some light on my problem. And guess what, I got a solution within 24 hours !!!!! That isn’t even damn fast, that is just amazingly brilliant !! His original answer is situated here.
Thanks again Shawn !!

After looking at the solution, there are 2 lesson to be made :

1. Not everything can be done with .Net ( so hence the reason why I got stuck 🙂 )
2. Not all solutions need to be difficult ( just one Windows script command line does exactly what we wanted )

Now for all other people out there who would stumble upon this same problem, here is the code I used in .Net to launch the script, enjoy.


public void StoreCultureInfo(int lcid)
{
try
{
//First create a CultureInfo object from the given LCID code
CultureInfo cultureInfo = CultureInfo.GetCultureInfo(lcid);

//Setting the .Default user in the Registry can't be done through .Net code in a straightforward manner !
//But there is a way to do this through use of command line syntax, the details can be found here : http://support.microsoft.com/default.aspx?scid=kb;en-us;289125
//Reminder : This option will only work for Windows XP, in Windows Vista detailed solution can be found here : http://www.microsoft.com/globaldev/vista/vista_tools/vista_command_line_international_configuration.mspx

//First we need to create a text file with the correct Locale for the Default user
//The locale should be specified in Hexadecimal format !
string hexadecimalLCID = cultureInfo.LCID.ToString("X");
string localeConfigurationFile = Path.Combine(PathHelper.TempDirectory, "regopts.txt");
//Be sure this configuration file doesn't exist yet
if (File.Exists(localeConfigurationFile))
FileHelper.DeleteFileEvenIfReadonly(localeConfigurationFile);

using (StreamWriter streamWriter = File.CreateText(localeConfigurationFile))
{
streamWriter.WriteLine("[RegionalSettings]");
streamWriter.WriteLine("UserLocale_DefaultUser = {0}", hexadecimalLCID);
streamWriter.Close();
}

//Now execute the command line option to swith the default user locale !
Process process = new Process();
process.StartInfo.FileName = "rundll32.exe";
process.StartInfo.Arguments = string.Concat("shell32,Control_RunDLL intl.cpl,,/f:", "\"", localeConfigurationFile, "\"");
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();

int processExitCode = process.ExitCode;

if (processExitCode != 0)
throw new ApplicationException("Problem occured while setting the Default user locale !");
break;
}
}
catch (ApplicationException ex)
{
}
}

Technorati tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *