【问题标题】:Change system date,time更改系统日期、时间
【发布时间】:2023-09-28 19:56:01
【问题描述】:

您好,我知道之前有人问过这个问题,但我需要帮助更改 c# 中的系统日期时间。在进行谷歌搜索时,我发现了一个建议以下代码的网站

public struct SYSTEMTIME 
{    
    public ushort wYear,wMonth,wDayOfWeek,wDay, wHour,wMinute,wSecond,wMilliseconds;
}

[DllImport("kernel32.dll")]
public extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);

/// <param name="lpSystemTime">[in] Pointer to a SYSTEMTIME structure that
/// contains the current system date and time.</param>
[DllImport("kernel32.dll")]
public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);

static void Main()
{    
    Console.WriteLine(DateTime.Now.ToString());
    SYSTEMTIME st = new SYSTEMTIME();
    GetSystemTime(ref st);
    Console.WriteLine("Adding 1 hour...");
    st.wHour = (ushort)(st.wHour + 1 % 24);
    if (SetSystemTime(ref st) == 0)
        Console.WriteLine("FAILURE: SetSystemTime failed");
    Console.WriteLine(DateTime.Now.ToString());
    Console.WriteLine("Setting time back...");
    st.wHour = (ushort)(st.wHour - 1 % 24);
    SetSystemTime(ref st);
    Console.WriteLine(DateTime.Now.ToString());
    Console.WriteLine("Press Enter to exit");
    Console.Read();
}

但是当我在我的系统中运行它时,它显示当前日期/时间没有变化。我应该进行任何更改吗?
编辑:收到消息 FAILURE: SetSystemTime failed when I try to run

【问题讨论】:

  • 这是例如测试目的?在这种情况下,最好将时钟抽象出来(因此,删除对DateTime.Now 的任何直接调用)而不是摆弄系统的实际时间。
  • 此代码更改日期并恢复它。除非您将“FAILURE:SetSystemTime failed”发送到控制台 - 时间已成功更改.. 但是几毫秒.. 删除“时间恢复部分”,并根据您的需要调整代码。
  • @Damien_The_Unbeliever 实际上有一个程序应该为用户指定的特定年份生成数据。认为只要用户想要该特定年份的消息,就可以更改系统日期时间
  • 您想在哪个版本的 Windows 上运行此代码?因为我认为用户不是管理员组的一部分,所以它可能不起作用。
  • @BlackFrog 主要用于测试真正...在 windows xp 中

标签: c# date time


【解决方案1】:

你应该使用 coredll.dll 来存档这个..

[DllImport("coredll.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);


private struct SYSTEMTIME 
{
    public ushort wYear;
    public ushort wMonth; 
    public ushort wDayOfWeek; 
    public ushort wDay; 
    public ushort wHour; 
    public ushort wMinute; 
    public ushort wSecond; 
    public ushort wMilliseconds; 
}

private void GetTime()
{
    // Call the native GetSystemTime method 
    // with the defined structure.
    SYSTEMTIME stime = new SYSTEMTIME();
    GetSystemTime(ref stime);

    // Show the current time.           
    MessageBox.Show("Current Time: "  + 
        stime.wHour.ToString() + ":"
        + stime.wMinute.ToString());
}
private void SetTime()
{
    // Call the native GetSystemTime method 
    // with the defined structure.
    SYSTEMTIME systime = new SYSTEMTIME();
    GetSystemTime(ref systime);

    // Set the system clock ahead one hour.
    systime.wHour = (ushort)(systime.wHour + 1 % 24);
    SetSystemTime(ref systime);
    MessageBox.Show("New time: " + systime.wHour.ToString() + ":"
        + systime.wMinute.ToString());
}

我没有测试过。但我希望它会起作用

【讨论】:

  • 是kernel32.dll导出的函数。它在 coredll.dll 中不存在。 TS 示例中的代码完全可以正常工作。
  • @rufanov 它早对我有用.. 这是来自 msdn 文档 msdn.microsoft.com/en-us/library/ms172517(v=vs.90).aspx
  • coredll 一个正在抛出异常,但...说它不受支持
  • @Chandan Pasunoori,在此文档中清楚地写道,它仅在当前废弃的“Microsoft .NET Compact Framework”项目中可用 - 非常受限的 .NET Framework 版本,也已在过时的 Windows CE 6 上可用(和 Windows Mobile 6)在 2006-2009 年。