How to set System Date in C#

Discussion in 'Microsoft C# .NET' started by Indrani, Aug 9, 2005.

  1. Indrani

    Indrani Guest

    I am working on a C# console application. How can we set the system
    date in C#.

    Rgds,
    Indrani
     
    Indrani, Aug 9, 2005
    #1
    1. Advertisements

  2. Indrani,

    I have to say, the console application you want to make is pretty
    difficult, since you have to somehow access the user's computer's time
    settings.
    If you were to distrubute your application for other people to run, I
    don't think that they would like it if you changed their computer's
    time, so perhaps your 'goal' might be possibly impossible.

    However, I figured out how to represent a time; use the DateTime class.
    There are 12 constructors for it, so you can be really precise. Here's
    an example:

    DateTime dt = new DateTime(2005, 8, 9);

    Of course, their are quite are number of methods to this class, which
    include adding, subtracting, toString, etc.

    Hope this helps,

    Seen Sharp
     
    Visually Seen #, Aug 9, 2005
    #2
    1. Advertisements

  3. "Indrani" <> wrote in message
    news:...
    >I am working on a C# console application. How can we set the system
    > date in C#.
    >
    > Rgds,
    > Indrani
    >



    1. Use PInvoke to call Win32 API SetSystemTime, or
    2. use System.Management classes with WMI class Win32_OperatingSystem and
    call SetDateTime on that class.

    Both require that the caller has been granted SeSystemTimePrivilege and that
    this privilege is enabled.
    The second is only available on XP and higher.


    Willy.
     
    Willy Denoyette [MVP], Aug 9, 2005
    #3
  4. "Willy Denoyette [MVP]" <> wrote in message
    news:...
    >
    > "Indrani" <> wrote in message
    > news:...
    >>I am working on a C# console application. How can we set the system
    >> date in C#.
    >>
    >> Rgds,
    >> Indrani
    >>

    >
    >
    > 1. Use PInvoke to call Win32 API SetSystemTime, or
    > 2. use System.Management classes with WMI class Win32_OperatingSystem and
    > call SetDateTime on that class.
    >
    > Both require that the caller has been granted SeSystemTimePrivilege and
    > that this privilege is enabled.
    > The second is only available on XP and higher.
    >
    >
    > Willy.
    >
    >


    Here is how using PInvoke...

    class Tester
    {
    [System.Runtime.InteropServices.DllImport("kernel32", SetLastError =
    true)]
    private static extern bool GetSystemTime(out SYSTEMTIME systemTime);
    [System.Runtime.InteropServices.DllImport("kernel32", SetLastError =
    true)]
    private static extern bool SetSystemTime(ref SYSTEMTIME systemTime);
    struct SYSTEMTIME {
    internal short wYear;
    internal short wMonth;
    internal short wDayOfWeek;
    internal short wDay;
    internal short wHour;
    internal short wMinute;
    internal short wSecond;
    internal short wMilliseconds;
    }
    static void Main()
    {
    SYSTEMTIME st;
    if(GetSystemTime(out st))
    {
    st.wHour = 13; //Beware SYSTEMTIME is in UTC time format!!!!!
    if(SetSystemTime(ref st))
    Console.WriteLine("success");
    else
    Console.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
    }
    else
    Console.WriteLine("GetSystemTime failed: {0}",
    System.Runtime.InteropServices.Marshal.GetLastWin32Error());
    }
    }

    Willy.
     
    Willy Denoyette [MVP], Aug 9, 2005
    #4
  5. Indrani

    Indrani Guest

    Indrani, Aug 9, 2005
    #5
  6. Hi,

    And this solved the OP how? :)

    Beside Willy post ( which does indeed solve the problem ) there is a site
    http://www.pinvoke.net where you can find a huge collection of signatures to
    p/invoke


    cheers,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation



    "Visually Seen #" <> wrote in message
    news:...
    > Indrani,
    >
    > I have to say, the console application you want to make is pretty
    > difficult, since you have to somehow access the user's computer's time
    > settings.
    > If you were to distrubute your application for other people to run, I
    > don't think that they would like it if you changed their computer's
    > time, so perhaps your 'goal' might be possibly impossible.
    >
    > However, I figured out how to represent a time; use the DateTime class.
    > There are 12 constructors for it, so you can be really precise. Here's
    > an example:
    >
    > DateTime dt = new DateTime(2005, 8, 9);
    >
    > Of course, their are quite are number of methods to this class, which
    > include adding, subtracting, toString, etc.
    >
    > Hope this helps,
    >
    > Seen Sharp
    >
     
    Ignacio Machin \( .NET/ C# MVP \), Aug 9, 2005
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jabadia
    Replies:
    1
    Views:
    707
    Daniel O'Connell [C# MVP]
    Feb 14, 2004
  2. Rock
    Replies:
    0
    Views:
    586
  3. Jim H
    Replies:
    3
    Views:
    1,662
    Jim H
    Nov 13, 2004
  4. ABC
    Replies:
    0
    Views:
    285
  5. Mike

    How to set set an autoincremntal option

    Mike, Nov 23, 2007, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    183
Loading...

Share This Page