0

I try to write some C# code to change time in my computer. This is the Main function of the console application I have. I compiled the code and get an exe file named TimeChange.exe

[StructLayoutAttribute(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
    public short year;
    public short month;
    public short dayOfWeek;
    public short day;
    public short hour;
    public short minute;
    public short second;
    public short milliseconds;
}

[DllImport("kernel32.dll")]
static extern bool SetLocalTime(ref SYSTEMTIME time);
private static void Main(string[] args)
{
    SYSTEMTIME st;
    DateTime trts = DateTime.Now + new TimeSpan(0, 0, 10, 0);

    st.year = (short)trts.Year;
    st.month = (short)trts.Month;
    st.dayOfWeek = (short)trts.DayOfWeek;
    st.day = (short)trts.Day;
    st.hour = (short)trts.Hour;
    st.minute = (short)trts.Minute;
    st.second = (short)trts.Second;
    st.milliseconds = (short)trts.Millisecond;
    Console.WriteLine("Setting time");
    SetLocalTime(ref st);
    Console.WriteLine("Setting finished");
    Console.ReadLine();
}

However, I found that I need to be Administrator before I can change the time. I am sure if I right click the compiled exe file and select Run as Administrator, the time can be changed. After doing some search, I found that CPAU can help to run an application as an Administrator. Then I downloaded CPAU and put the CPAU in the same folder as the TimeChange.exe. In Windows command line (not in Administrator mode), I ran

CPAU -u Username -p Password -ex "TimeChange.exe" -wait -LWP

In the command line, it shown that

CPAU V01.11.00cpp Joe Richards (joe@joeware.net) November 2005

Current Security Context: Group-PC\Username
Process Created...

The command completed successfully.

However, the time was not changed.

Can anyone help to see what is the possible problem? Thank you.

Ben
  • 957
  • 1
  • 11
  • 37
  • Have you tested TimeChange.exe from the command line, i.e. without involving C# or any code? – rory.ap Nov 03 '15 at 13:43
  • Yes, I tested it. I right clicked TimeChange.exe and select Run as an Administrator in the menu. Then a confirm window popped up. I clicked yes and then the time in my system was changed. – Ben Nov 03 '15 at 13:51
  • You can [add something to the manifest](http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7) to make your program run as Administrator, this will force the UAC prompt to show up when you run your program. – Ron Beyer Nov 03 '15 at 13:57

1 Answers1

1

Instead of external programs (which by the way, runas can also do it and it's built in), the proper way to do it is to simply require administrative priviledges on application startup. Simply add the following to your manifest file:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Hi Blindy. Thank you for your comment. However, even if I set this parameter, I still need to click the yes button in the popup window. What I want to do is to run the console application without clicking anything because I plan to run the application in a computer without mouse, keyboard and monitor. Thank you. – Ben Nov 03 '15 at 22:55
  • You could always disable UAC, but you still need to request administrative access to the system using the above. That would cover all your requirements! – Blindy Nov 03 '15 at 23:59