CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Change System Date/Time with C#

  1. #1
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    50

    Change System Date/Time with C#

    How to?
    session™
    //Developing the Future
    //Power of the Vision

  2. #2
    Join Date
    Aug 2002
    Location
    Germany, Berlin
    Posts
    60
    Just start the extern process cmd.exe with the time argument, i.e.
    String arg = @"/C \"time hh:min:sec\"";

    Similar with date:
    extern process : cmd.exe
    arg =@ "/C \"data dd-mm-[yy]yy\"";

    Hope this helps

  3. #3
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    50
    Originally posted by Holiday
    Just start the extern process cmd.exe with the time argument, i.e.
    String arg = @"/C \"time hh:min:sec\"";

    Similar with date:
    extern process : cmd.exe
    arg =@ "/C \"data dd-mm-[yy]yy\"";

    Hope this helps
    can you please explain a lil bit more???
    session™
    //Developing the Future
    //Power of the Vision

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    it is better way to use
    P/Invoke functions using Win32 API.

    define this structure in your form's main class
    ------------------------ [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME
    {
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;
    }

    [DllImport("kernel32.dll", SetLastError=true)]
    public static extern bool SetSystemTime( [In] ref SYSTEMTIME st );


    ----------------------


    now somewhere you can test following

    SYSTEMTIME st = new SYSTEMTIME();
    st.wYear = 2003; // must be short
    st.wMonth = 5;
    st.wDay = 22;
    st.wHour = 0;
    st.wMinute = 0;
    st.wSecond = 0;

    SetSystemTime(ref st); // invoke this method.

    -hope this helps,
    Paresh
    - Software Architect

  5. #5
    Join Date
    Aug 2002
    Location
    Germany, Berlin
    Posts
    60
    Originally posted by session
    can you please explain a lil bit more???
    The way parshgh proposed is the more sophisticated. You should use this way.
    Just for satisfying your request:

    args as in the first post!

    string command= "cmd";
    string args = string.Format("/C \"{0}\"",argument.Trim());

    System.Diagnostics.ProcessStartInfo psi =
    new System.Diagnostics.ProcessStartInfo(command,args);

    psi.UseShellExecute = false;
    System.Diagnostics.Process proc = null;
    proc = System.Diagnostics.Process.Start(psi);

  6. #6
    Join Date
    Aug 2006
    Posts
    2

    Re: Change System Date/Time with C#

    Quote Originally Posted by pareshgh
    it is better way to use
    P/Invoke functions using Win32 API.

    define this structure in your form's main class
    ------------------------ [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME
    {
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;
    }

    [DllImport("kernel32.dll", SetLastError=true)]
    public static extern bool SetSystemTime( [In] ref SYSTEMTIME st );


    ----------------------


    now somewhere you can test following

    SYSTEMTIME st = new SYSTEMTIME();
    st.wYear = 2003; // must be short
    st.wMonth = 5;
    st.wDay = 22;
    st.wHour = 0;
    st.wMinute = 0;
    st.wSecond = 0;

    SetSystemTime(ref st); // invoke this method.

    -hope this helps,
    Paresh






    -------------------------------------------------------
    Hi
    I want to use your code in my project too. but I don't know where I have to write these parts
    would you please give me a single example of using these instructions in C#.NET 2005 ? I really need this so emergency
    I'm looking forward your respond
    thanks

  7. #7
    Join Date
    Aug 2006
    Posts
    2

    Re: Change System Date/Time with C#

    what's the meaning of this error?????????????

    Error 2 The type or namespace name 'StructLayout' could not be found (are you missing a using directive or an assembly reference?) F:\Documents and Settings\admin\My Documents\Visual Studio 2005\Projects\changesystemtime\changesystemtime\Form1.cs 13 12 changesystemtime

  8. #8
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Change System Date/Time with C#

    Add this line at the top of your code where you use StructLayout attribute.
    Code:
    using System.Runtime.InteropServices;
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  9. #9
    Join Date
    Jan 2008
    Posts
    1

    Exclamation Re: Change System Date/Time with C#

    Quote Originally Posted by Holiday

    extern process :cmd.exe arg
    arg=@ "/C \"data dd-mm-[yy]yy\"";

    this is what language code i cant run it in C# can you write the whole function
    instead of writing snapet

    process: cmd .exe

    does not support by Process class
    argumets also

    plz tell breafly

    plz explan it.

  10. #10
    Join Date
    May 2020
    Posts
    1

    Re: Change System Date/Time with C#

    Quote Originally Posted by nidostyle View Post
    plz explan it.
    How do I reset the system datetime back to the current date time?

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,277

    Re: Change System Date/Time with C#

    Quote Originally Posted by TonyPag View Post
    How do I reset the system datetime back to the current date time?
    It the computer connected to a network domain?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)