Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I had previously made an app to change windows time in c#, to a previous time, and it used to work fine on windows 7, but now on windows 10 it will just not work.

What I have tried:

C#
private void button1_Click_1(object sender, EventArgs e)
        {
            
            h1 = h;//hours
            m1 = m;//minutes
            s1 = s;//seconds
            //booleano is my boolean value that starts in true
            //MessageBox.Show(""+booleano);
            if (booleano)
            {
                foreach (String st in tokens1)
                {
                    time = tokens1[0] + "," + tokens1[1] + "," + tokens1[2] + "," + h1 + "," + m1 + "," + s1 + "," + false.ToString() + "," + true.ToString();
                }

                System.IO.File.WriteAllText(Application.StartupPath + "\\time.txt", time);
                //MessageBox.Show(Application.StartupPath);               
                st.wYear = short.Parse(""+2014); // must be short
                st.wMonth = short.Parse(""+1);
                st.wDay = short.Parse(""+1);
                SetSystemTime(ref st);
                booleano = false;
            }
            else
            {
                booleano = true;
                FileStream file = new FileStream(Application.StartupPath+"\\time.txt", FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(file);
                sr.ReadLine();
                var textLines = File.ReadAllLines(Application.StartupPath + "\\time.txt");
                foreach (var line in textLines)
                {
                    string[] dataArray = line.Split(',');
                    st.wYear= short.Parse(dataArray[0]);
                    st.wMonth = short.Parse(dataArray[1]);
                    st.wDay = short.Parse(dataArray[2]);
                    st.wHour = short.Parse(dataArray[3]);
                    st.wMinute = short.Parse(dataArray[4]);
                    st.wSecond = short.Parse(dataArray[5]);                    
                }
                file.Close();
                SetSystemTime(ref st);
            }
            
        }

This is the code I have on a button to change time
Posted
Updated 22-Apr-21 22:08pm
Comments
Jochen Arndt 20-Apr-17 6:35am    
Is the application executed with administrator privileges?

If not you have to give the account the required privileges.
With Windows 10 this can be done using the group policy editor which is only available with Pro and Enterprise versions.

1 solution

First off, you dont; want to do that - there is no good reason for altering the time, since most Windows installations now use a NTP server by default to maintain the system time accurately - and the NTP server will override your app anyway.

In addition, as Jochen said, you need admin privileges to change the system time on Win10, partly because of the use of NTP servers, and partly because keeping the time right is increasingly important to applications which timestamp data.

The main reasons for playing with the system date and / or time is to bypass trial software expiry periods - and that is not a legitimate usage. We do not condone, support, or assist in the production of malicious code in any way, form, or manner. This is a professional site for professional developers. So unless you have a very legitimate use you can explain to us, you will not get help here.
 
Share this answer
 
Comments
Member 10850253 20-Apr-17 15:47pm    
How would I add admin privileges to my app programatically on app start?
Gonzalo Méndez Díaz 18-Jul-18 17:24pm    
In my case, I need to allow end users of tablets running windows 10 to change the date and time settings. They can't even quit the application.
Im my opinion, if somebody knows the answer and wants to help, they just should provide the answer without judging if there is a good reason or not for doing that. There could be millions of legitimate reasons for executing a requirement, and if they want to change the date and time for cheating licesing controls, they don't need to create an application for that.
Member 10744185 27-Apr-19 22:26pm    
Reason for changing the system time? Because some Windows IOT device's (eg, Raspberry Pi) don't have a real-time clock and you need to set the system time every time you power it up. Perfectly legit, I believe

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