Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
i want to change System date using Registry

What I have tried:

using Microsoft.Win32;
  private void button3_Click(object sender, EventArgs e)
     {
         RegistryKey regkey =
         Registry.CurrentUser.OpenSubKey(@"ControlPanel\International",
         true);//Not sure about this path i am using window 10 pro
         regkey.SetValue("sShortDate", "12/30/2023");//the date i want
         regkey.SetValue("sLongDate", "12/30/2023");
     }
Posted
Updated 31-Jul-23 4:32am
v3
Comments
Graeme_Grant 31-Jul-23 0:54am    
The real question is WHY???
Engineer khalid 31-Jul-23 3:40am    
If computer set to different language like arabic the format of dat will be dd\mm\yyyy not mm\dd\yyyy
if the date 1\1\2023 for example it would be impossible to know which one is day and which one is month.i want to save the date of the system in string then try to set the date to 1\13\2023 if i catch then the format is dd\mm\yyyy if not it should be mm\dd\yyyy then i soon reset the date back, my goal is to know the date format of the user computer
if there is away to know the type of format (using Bios or any method) let me know.
Many thanks
Richard MacCutchan 31-Jul-23 6:42am    
The sShortDate field specifies the format of the date, not its actual value. So if you want it to show the in day-month order, it needs to be set to "dd/MM/yyyy". Similar values must be used for the sLongDate field.
Richard Deeming 1-Aug-23 4:40am    
But as Griff said, any app that changes the date format on my machine without permission is getting uninstalled and blocked PDQ. :)
Richard MacCutchan 1-Aug-23 4:44am    
I was not suggesting that it is a good idea to do this.

To add to what Dave has said, not only do you need Admin rights to change them but even if you could change the system date and / or time, or the system date format via the registry, it would be a very bad idea as it would affect the whole machine, not just your app - and that will very quickly alienate your users.

Mess with my date or date format and your app will be uninstalled pretty quickly, and an invoice will be winging your way for the software licences that suddenly decided they were expired and needed more money from me ...

Why you would want to do that I have no idea - but you need to find a better solution that this, even if it did work!
 
Share this answer
 
To know what date format a user's machine is set to, you can use the 'CultureInfo' class and the 'DateTimeFormat' properties. You CAN NOT change a user's machine date time formats, you will not have many users left within the first day of your app - MS Learn | CultureInfo Class[^]

Quote:
The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.


To use this in code to determine the user's date format -
C#
using System;
using System.Globalization;

//Get the current culture of the user's machine...
CultureInfo userCulture = CultureInfo.CurrentCulture;

//Get the date format string based on the user's culture...
string dateFormat = userCulture.DateTimeFormat.ShortDatePattern;


Assuming you want to use the date in a string, you can use the 'DateTime' class to get the current date and then format it using the above determined 'dateFormat' -
C#
using System;
using System.Globalization;

//Get the current culture of the user's machine...
CultureInfo userCulture = CultureInfo.CurrentCulture;

//Get the date format string based on the user's culture...
string dateFormat = userCulture.DateTimeFormat.ShortDatePattern;

//Your used string...
string myString = "This is the date: ";

//Get the current date and time...
DateTime currentDate = DateTime.Now;

//Format the date using the user's date format...
string formattedDate = currentDate.ToString(dateFormat);

//Combine the used string and the formatted date...
string resultString = myString + formattedDate;

//Show the result
Console.WriteLine(resultString);


No messing with a users settings or his machine...
 
Share this answer
 
Kind of odd because you cannot change the system date in the registry. It looks like you're trying to change the format of the date presented to whatever date you want and that's not going to work.

But, I see one glaring problem right off the bat. In your registry path, it should be "Control Panel", not "ControlPanel".
 
Share this answer
 
Comments
cmarcotte 30-Jul-23 21:18pm    
And, I think it also needs run the code as administrator.
Dave Kreskowiak 30-Jul-23 22:44pm    
You can change stuff under Current User, but it's not going to change the system time.

For actually changing the time, you need admin rights on the machine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900