Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
hello...
i am doing set my software trail version ...so how to set the date and time but ca't affect system time changing.in .net c# windows application
....
Posted
Comments
Philippe Mori 13-Aug-15 12:27pm    
No application should ever change system time.

If I understood correctly, you want to expire the trial version based on the date. If that is correct you don't need (shouldn't) to change the system clock but to check if the trial period has ended.

One example to create a trial version: Application Trial Maker[^]
 
Share this answer
 
I think the first question to ask here is why do you want to set the System Time, and how does that relate to your making a trial version of an application. Most users set the System Time by automatically synchronizing with on-line Time Providers, these days.

I, and I think a lot of other users, would be quite upset by any application, particularly a trial version, changing their System DateTime down at the fundamental Windows' internals level !

If what you really want to is compare the real system time with the install date/time of your trial app: well then make your trial app fetch the real current time, based on the current time zone of the machine your trial app is installed on ... if what you are worried about is the end-user extending the period of trial by their setting their System DateTime manually back in time.

However: you asked a specific question: here's the best answer I can come up with:

This is going to require you use API calls from C#.

First, you must have the security permissions required to change the system date/time: specifically the SE_SYSTEMTIME_NAME permission. For information about using special permissions:[^].

Here's the MSDN example that shows you how to do this, via API calls:[^].

For a C# example of how to use these API calls, see:[^].

This CP article deals with how to achieve Administrator privileges in Vista, but may be relevant:[^]

Disclaimer: I have never needed to do this myself, so the resources above have not been personally used and tested.
 
Share this answer
 
To get/set the system date & time using unmanaged code directly, the below code is used:

C#
public struct SystemTime
{
    public ushort Year;
    public ushort Month;
    public ushort DayOfWeek;
    public ushort Day;
    public ushort Hour;
    public ushort Minute;
    public ushort Second;
    public ushort Millisecond;
};

[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
public extern static void Win32GetSystemTime(ref SystemTime sysTime);

[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
public extern static bool Win32SetSystemTime(ref SystemTime sysTime);

private void button1_Click(object sender, EventArgs e)
{
    // Set system date and time
    SystemTime updatedTime = new SystemTime();
    updatedTime.Year = (ushort)2012;
    updatedTime.Month = (ushort)2;
    updatedTime.Day = (ushort)3;
    updatedTime.Hour = (ushort)10;
    updatedTime.Minute = (ushort)10;
    updatedTime.Second = (ushort)0;
    Win32SetSystemTime(ref updatedTime);
}


If you want to avoid the changes at system level, you can ignore this type of call. Instead, you can use your own timer control.
 
Share this answer
 
C#
void setDate(string dateInYourSystemFormat)
    {
        var proc = new System.Diagnostics.ProcessStartInfo();
        proc.UseShellExecute = true;
        proc.WorkingDirectory = @"C:\Windows\System32";
        proc.CreateNoWindow = true;
        proc.FileName = @"C:\Windows\System32\cmd.exe";
        proc.Verb = "runas";
        proc.Arguments = "/C date " + dateInYourSystemFormat;
        try
        {
            System.Diagnostics.Process.Start(proc);
        }
        catch
        {
            MessageBox.Show("Error to change time of your system");
            Application.ExitThread();
        }
    }
void setTime(string timeInYourSystemFormat)
    {
        var proc = new System.Diagnostics.ProcessStartInfo();
        proc.UseShellExecute = true;
        proc.WorkingDirectory = @"C:\Windows\System32";
        proc.CreateNoWindow = true;
        proc.FileName = @"C:\Windows\System32\cmd.exe";
        proc.Verb = "runas";
        proc.Arguments = "/C time " + timeInYourSystemFormat;
        try
        {
            System.Diagnostics.Process.Start(proc);
        }
        catch
        {
            MessageBox.Show("Error to change time of your system");
            Application.ExitThread();
        }
    }
 
Share this answer
 
v2

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