Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Get Regional Language settings.

Get Regional Language settings.

Scheduled Pinned Locked Moved ASP.NET
windows-adminhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mugdha_Aditya
    wrote on last edited by
    #1

    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

    B J 2 Replies Last reply
    0
    • M Mugdha_Aditya

      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

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Mugdha_Aditya

        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

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        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

        M 2 Replies Last reply
        0
        • J jkirkerx

          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

          M Offline
          M Offline
          Mugdha_Aditya
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • M Mugdha_Aditya

            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

            J Offline
            J Offline
            jkirkerx
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • J jkirkerx

              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

              M Offline
              M Offline
              Mugdha_Aditya
              wrote on last edited by
              #6

              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. ;(

              J 1 Reply Last reply
              0
              • M Mugdha_Aditya

                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. ;(

                J Offline
                J Offline
                jkirkerx
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups