Get Regional Language settings.
-
Hello, I wants to retrive regional language settings in my code. For that i used WMI. I tried below code. in page load : GetLoggedInUserCulture();
private static CultureInfo GetLoggedInUserCulture() { string folderName = string.Empty; string sID = GetWindowsLoggedInUserSID(); object locale = Registry.Users.OpenSubKey(sID + @"\\Control Panel\\International").GetValue("Locale"); int lCID = int.Parse(locale.ToString(), NumberStyles.HexNumber); CultureInfo ci = new CultureInfo(lCID); return ci; } private static string GetWindowsLoggedInUserSID() { string userName = null; string sID = null; try { ManagementScope oMs = new ManagementScope(); ObjectQuery oQuery = new ObjectQuery("Select UserName from Win32\_ComputerSystem"); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get(); ManagementClass mc = new ManagementClass(oMs, new ManagementPath("ServerBinding"), null); foreach (ManagementObject oReturn in oReturnCollection) { userName = oReturn\["UserName"\].ToString().ToLower(); } userName = userName.Substring(userName.LastIndexOf(@"\\") + 1); oQuery = new ObjectQuery("Select SID, Name from Win32\_Account where Name = '" + userName + "'"); oSearcher = new ManagementObjectSearcher(oMs, oQuery); oReturnCollection = oSearcher.Get(); foreach (ManagementObject oReturn in oReturnCollection) { if (oReturn\["SID"\] != null) { sID = oReturn\["SID"\].ToString(); } } } catch (Exception) { throw; } return sID; }
but oReturn["UserName"] and oReturn["SID"] return me null value. :( Do i need to add anything in webconfig. or any thing else am missing.. please help me
-
Hello, I wants to retrive regional language settings in my code. For that i used WMI. I tried below code. in page load : GetLoggedInUserCulture();
private static CultureInfo GetLoggedInUserCulture() { string folderName = string.Empty; string sID = GetWindowsLoggedInUserSID(); object locale = Registry.Users.OpenSubKey(sID + @"\\Control Panel\\International").GetValue("Locale"); int lCID = int.Parse(locale.ToString(), NumberStyles.HexNumber); CultureInfo ci = new CultureInfo(lCID); return ci; } private static string GetWindowsLoggedInUserSID() { string userName = null; string sID = null; try { ManagementScope oMs = new ManagementScope(); ObjectQuery oQuery = new ObjectQuery("Select UserName from Win32\_ComputerSystem"); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get(); ManagementClass mc = new ManagementClass(oMs, new ManagementPath("ServerBinding"), null); foreach (ManagementObject oReturn in oReturnCollection) { userName = oReturn\["UserName"\].ToString().ToLower(); } userName = userName.Substring(userName.LastIndexOf(@"\\") + 1); oQuery = new ObjectQuery("Select SID, Name from Win32\_Account where Name = '" + userName + "'"); oSearcher = new ManagementObjectSearcher(oMs, oQuery); oReturnCollection = oSearcher.Get(); foreach (ManagementObject oReturn in oReturnCollection) { if (oReturn\["SID"\] != null) { sID = oReturn\["SID"\].ToString(); } } } catch (Exception) { throw; } return sID; }
but oReturn["UserName"] and oReturn["SID"] return me null value. :( Do i need to add anything in webconfig. or any thing else am missing.. please help me
That code is running on the server. That means that your user must be a user of that server or the domain the server is a member of. But generally, users of a web application are different from the Windows user. Better use cookies or a database entry for storing the language/regional settings. Show your page in a default language of your choice, and let the user decide which language he prefers, and store that information.
-
Hello, I wants to retrive regional language settings in my code. For that i used WMI. I tried below code. in page load : GetLoggedInUserCulture();
private static CultureInfo GetLoggedInUserCulture() { string folderName = string.Empty; string sID = GetWindowsLoggedInUserSID(); object locale = Registry.Users.OpenSubKey(sID + @"\\Control Panel\\International").GetValue("Locale"); int lCID = int.Parse(locale.ToString(), NumberStyles.HexNumber); CultureInfo ci = new CultureInfo(lCID); return ci; } private static string GetWindowsLoggedInUserSID() { string userName = null; string sID = null; try { ManagementScope oMs = new ManagementScope(); ObjectQuery oQuery = new ObjectQuery("Select UserName from Win32\_ComputerSystem"); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get(); ManagementClass mc = new ManagementClass(oMs, new ManagementPath("ServerBinding"), null); foreach (ManagementObject oReturn in oReturnCollection) { userName = oReturn\["UserName"\].ToString().ToLower(); } userName = userName.Substring(userName.LastIndexOf(@"\\") + 1); oQuery = new ObjectQuery("Select SID, Name from Win32\_Account where Name = '" + userName + "'"); oSearcher = new ManagementObjectSearcher(oMs, oQuery); oReturnCollection = oSearcher.Get(); foreach (ManagementObject oReturn in oReturnCollection) { if (oReturn\["SID"\] != null) { sID = oReturn\["SID"\].ToString(); } } } catch (Exception) { throw; } return sID; }
but oReturn["UserName"] and oReturn["SID"] return me null value. :( Do i need to add anything in webconfig. or any thing else am missing.. please help me
-
Good Idea, and nice shot with the code. But reading the registry from a web app is a no no. May work in the web dev (F5), but not in production mode on a production server. The post above is correct. Let the user choose, then write the cookie
thanks for the reply :) but its client requirement that they want to disply data acooring to regional setting language. :( My code is working for another project but its not working in another porject where actually i need language. ;( not sure why so
-
thanks for the reply :) but its client requirement that they want to disply data acooring to regional setting language. :( My code is working for another project but its not working in another porject where actually i need language. ;( not sure why so
Go back to the beginning, and check the registry value, you may be able to get the value from the browser using javascript / jquery / json, and transmit the value back for storage. Just keep stepping through the process until something clicks. Check your Global call for the culture value, and make sure it matches your select case or switch, or route mapping. No insult intended, but I think that will backfire on you later on down the road. ASP.net keeps tightening the security screws, so one day on a server update, it may fail at like 3am in the morning. Then you will flash back at my warning, and slap yourself in the head.
-
Good Idea, and nice shot with the code. But reading the registry from a web app is a no no. May work in the web dev (F5), but not in production mode on a production server. The post above is correct. Let the user choose, then write the cookie
i tries to get regional settings in javascript , it workes but javascript runs after pageload. i iwant that value before page load done. i want to apply that language 1st time. i tried to store that valaue in javascript but it gets after page load. ;(
-
i tries to get regional settings in javascript , it workes but javascript runs after pageload. i iwant that value before page load done. i want to apply that language 1st time. i tried to store that valaue in javascript but it gets after page load. ;(
You can run javascript as client startup script - head tag, and with javascript reload the page with the correct culture, via url change, or set a session variable after that. You can use some magic like a modal popup saying sensing personal settings, and then make the switch. Use some artwork to make it look cool. First just get the mechanics working for after the culture is known, and then polish it it with culture detection. Those are the only Ideas I have.