none
Change System Date Fromate Permanently RRS feed

  • Question

  • Hi,

        I want to change System Date Format permanently when my application run.

    I wrote this code :-

    RegistryKey rkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\International", true);
              
                // Set value for short Date Format
                rkey.SetValue("MM/dd/yyyy", DateTime.Now.ToShortDateString() );

                // Set value for Long Date Format
                rkey.SetValue("MMMM dd, yyyy", DateTime.Now);
                rkey.Close();
                MessageBox.Show("System dateFormat Is changed");

    But It is not working please help me out.

    Thanks

     

    Wednesday, September 28, 2011 1:16 AM

Answers

  • [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME
    {
        public short wYear;
        public short wMonth;
        public short wDayOfWeek;
        public short wDay;
        public short wHour;
        public short wMinute;
        public short wSecond;
        public short wMilliseconds;
    }
    
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool SetSystemTime([In] ref SYSTEMTIME st);
    
    public void setSystemDateTimeInManaged(DateTime dt)
    {
        //now somewhere you can test following
        SYSTEMTIME st = new SYSTEMTIME();
        st.wYear   = (short)dt.Year;
        st.wMonth  = (short)dt.Month;
        st.wDay    = (short)dt.Day;
        st.wHour   = (short)dt.Hour;
        st.wMinute = (short)dt.Minute;
        st.wSecond = (short)dt.Second;
    
        SetSystemTime(ref st); // invoke this method.
    }
    


    Any fool can know. The point is to understand.(Albert Einstein)
    • Marked as answer by Paul Zhou Wednesday, October 5, 2011 6:27 AM
    • Unmarked as answer by Paul Zhou Thursday, October 6, 2011 2:55 AM
    • Marked as answer by mpspankaj Friday, February 10, 2012 3:16 PM
    Thursday, September 29, 2011 1:42 AM

All replies

  • If you change the international settings in registry, it's natural to require re-logon to be in effect.

    Even the KB article suggest this.

    While you may try to broadcast WM_SETTINGCHANGE with lparam set to "Policy" (since this setting, as mentioned in the KB article, can be deployed by group policy), I don't know whether other applications will catch this setting and be ready to load with new date format. Your best bet is still to require logoff and logon again.

    EDIT: Found this thread have direct answer to your question. Just make sure you reference kernel32 instead of coredll. And you're still require to make WM_SETTINGCHANGE broadcast after that. (The reference suggest that it's internation change so maybe it should have lparam set to "intl" afterall.)


    • Edited by cheong00Editor Wednesday, September 28, 2011 3:53 AM
    • Marked as answer by Paul Zhou Wednesday, October 5, 2011 6:27 AM
    • Unmarked as answer by Paul Zhou Thursday, October 6, 2011 2:55 AM
    Wednesday, September 28, 2011 3:43 AM
    Answerer
  • Did you restart your machine?
    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, September 28, 2011 6:14 AM
  • Do you think making change to HKCU registry hive would require a restart?
    Wednesday, September 28, 2011 6:17 AM
    Answerer
  • Most of the registry settings are read b OS during boot up. In most of the cases, whatever changes made after system boot may not reflect until system is restarted. I did not test what he has said. But, that was a wild guess though.
    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, September 28, 2011 6:29 AM
  • [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME
    {
        public short wYear;
        public short wMonth;
        public short wDayOfWeek;
        public short wDay;
        public short wHour;
        public short wMinute;
        public short wSecond;
        public short wMilliseconds;
    }
    
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool SetSystemTime([In] ref SYSTEMTIME st);
    
    public void setSystemDateTimeInManaged(DateTime dt)
    {
        //now somewhere you can test following
        SYSTEMTIME st = new SYSTEMTIME();
        st.wYear   = (short)dt.Year;
        st.wMonth  = (short)dt.Month;
        st.wDay    = (short)dt.Day;
        st.wHour   = (short)dt.Hour;
        st.wMinute = (short)dt.Minute;
        st.wSecond = (short)dt.Second;
    
        SetSystemTime(ref st); // invoke this method.
    }
    


    Any fool can know. The point is to understand.(Albert Einstein)
    • Marked as answer by Paul Zhou Wednesday, October 5, 2011 6:27 AM
    • Unmarked as answer by Paul Zhou Thursday, October 6, 2011 2:55 AM
    • Marked as answer by mpspankaj Friday, February 10, 2012 3:16 PM
    Thursday, September 29, 2011 1:42 AM
  • Hi,

     

    Has your issue been resolved? Would you mind letting us know the result of the suggestions?

     

    Now I will mark an answer, you can mark others that you think to be so useful to your issue.

    If you still have any questions about this issue, please feel free to let me know. We will continue to work with you on this issue.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 5, 2011 6:27 AM
  • Hi Paul Zhou,

           I did not get all discussion and my issue has not solved. What exactly i want, when i run my application it automatically change system date format in dd/MM/yyyy or MM/dd/yyyy which i decided.

     

    Thanks

     

     

    Wednesday, October 5, 2011 3:01 PM
  • Call Marshal.GetLastWin32Error after my code and tell me returned value!

    Any fool can know. The point is to understand.(Albert Einstein)
    Wednesday, October 5, 2011 3:17 PM
  • Err... I don't think this answer the question.

    The original question is about System Datetime format but this function is for setting System Datetime.


    Thursday, October 6, 2011 1:26 AM
    Answerer
  • Have you checked whether the system date format is changed after logging off and logging on to Windows again?
    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 6, 2011 3:18 AM
  • Hello,

    I'm sorry for my understanding!

    Please open Run Command and enter regedit then follow: HKEY_CURRENT_USER\Control Panel\International. you can change above values by Microsoft.Win32.Registry Class!


    Any fool can know. The point is to understand.(Albert Einstein)
    Thursday, October 6, 2011 5:33 AM