1

I need to change/update/set my system (windows7) default dateTime format programatically and permanently until I change it myself through code or through windows GUI

I have tried a lot of solutions like this one from Code Project

       Console.Write(DateTime.Now + "");
       RegistryKey rkey = Registry.CurrentUser.OpenSubKey(@"
       Control Panel\International", true);
        rkey.SetValue("sShortDate", "dd/MM/yyyy");
        rkey.SetValue("sLongDate", "dd/MM/yyyy");
       Console.Write(DateTime.Now + "");

The closest and well reputed answer is following from from Set Default DateTime Format c#, I tried this solution but it did not help me. As It does not change my system datetime format (shown in taskbar). And after I restart the app having this code, I again get the old format before this code is executed.

using System;
using System.Globalization;
using System.Threading;

namespace test 
{
    public static class Program
    {
        public static void Main() {
          Console.Write(DateTime.Now + "");// MessageBox.Show(DateTime.Now + "");
          CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
          culture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
          culture.DateTimeFormat.LongTimePattern = "";
          Thread.CurrentThread.CurrentCulture = culture;
          Console.Write(DateTime.Now + "");// MessageBox.Show(DateTime.Now + "");
        }
    }
}
Community
  • 1
  • 1
Sami
  • 8,168
  • 9
  • 66
  • 99
  • Have a look at this SO post http://stackoverflow.com/questions/650849/change-system-date-programatically – Prabhu Murthy Oct 18 '12 at 10:14
  • Thanks @CodeIgnoto . But it is setting the `Date` and not the not the `Date Format` or `DateTime Format` I think – Sami Oct 18 '12 at 10:20
  • try System.IFormatProvider Interface.visit : http://www1.cs.columbia.edu/~lok/csharp/refdocs/System/types/IFormatProvider.html –  Oct 18 '12 at 10:50
  • @WajidAli Nai jani. SetFormat is needed instead of GetFormat. Also I am expecting some example. As I have been also looking lot of such things for more than two hours. But nothing has been truly helpful :( – Sami Oct 18 '12 at 11:02
  • @Sami: yar i have done lot of R&D for your problem but have not found any code snippet. –  Oct 18 '12 at 11:05
  • @WajidAli Thanks bro. I have got it. My code projects code was alright I just needed to restart explorer.exe to get my expected result. – Sami Oct 18 '12 at 11:25
  • Take a look at this question and its answer: http://social.msdn.microsoft.com/Forums/vstudio/en-US/263d73b2-8611-4398-9f09-9aa76bbf325e/hi-about-change-default-dateformat-windows-so?forum=vcgeneral – SepehrM Jul 23 '14 at 10:51

1 Answers1

1

I tried manually changing the registry keys mentioned in your question.

Here's what I did:

  • Pressed Win+R and type 'regedit' to get the Registry Editor.
  • Navigated the tree to Computer → HKEY_CURRENT_USER → Control Panel → International.
  • Double clicked the 'sShortDate' entry and changed the value to 'yyyy/MM/dd'.

I then opened Windows Explorer and all of the dates were shown in the new format, so this is definitely the right place in the registry.

I then tried the code you supplied to modify the registry and it, too, is changing the date shown in Explorer.

So, this leads me to believe that the Windows taskbar clock does not react to changes to this setting. I confirmed this by killing and restarting 'explorer.exe' from the Task Manager. If you restart Explorer you too should see the change take effect.


Edit: there appears to be no facility in .NET for setting the locale settings directly. You can, however, use P/Invoke to set it via the C++ Win API which should then cause the system clock (and other applications) to be notified of the change. See this discussion.

Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
  • @Sami: I appreciate this, which is why I went on to explain in my answer that the code you supplied is working too. Try restarting Explorer. – Paul Ruane Oct 18 '12 at 10:40
  • Sorry I have now understood to some extent what you have told. But i need to do this all through c#. Nothing with windows GUI – Sami Oct 18 '12 at 10:41
  • if you can please provide some lines to do your told procedure through c#. I will be grateful and it will be a complete and excellent answer to the question as well :) – Sami Oct 18 '12 at 10:43
  • @Sami: the lines are the *exact* ones in your question (from Code Project). Simply create a Console project and paste those exact lines into the `Main` method. – Paul Ruane Oct 18 '12 at 10:46
  • Thanks and +1 for your effort. Answer is making sense as well. But that code is not still doing the job. I have still my system datetime format as `MM/dd/yyyy` after executing only code project code – Sami Oct 18 '12 at 10:57
  • @Sami: did you restart explorer.exe? Where are you checking? – Paul Ruane Oct 18 '12 at 10:59
  • I had not restarted. But when I did I got the expected result. I know how to start it again System.Diagnostics.Process.Start("explorer.exe"), but i will search myself how to quit it. Thanks a lot for continuous good response :) – Sami Oct 18 '12 at 11:23
  • @Sami: that would work but the P/Invoke solution will definitely be the more elegant solution to this problem. Depends upon what the use-case is: I would not want to have Explorer be killed and restarted if this were going out to customers, for example. – Paul Ruane Oct 18 '12 at 13:00