【问题标题】:How to Get Current date time which is not Depended on System date time and No Required internet如何获取不依赖于系统日期时间且不需要互联网的当前日期时间
【发布时间】:2016-04-11 09:43:36
【问题描述】:

如何在 Windows 应用程序中使用 c# 获取不依赖于系统日期时间且不需要互联网的当前日期时间

【问题讨论】:

  • 如果不是来自系统或互联网,日期和时间从何而来?
  • 先生,我尝试制作桌面应用程序的试用版,所以用户是天才,他可以更改其系统日期,这样他就可以轻松地再次使用过期的应用程序,因此此应用程序取决于系统日期或时间跨度>
  • 免费试用次数比过期更容易

标签: c# asp.net wpf vb.net winforms


【解决方案1】:

答案是你不能。如果您不是从系统或互联网中选择时间,则无法获取当前日期时间。

【讨论】:

  • ohhhh ok 先生 ....How 可以试用版本哪些用户在更改系统日期后不能使用??
  • @RushangPatel 如果您想实施时间锁定,请询问。您提出的问题没有可用的答案。
【解决方案2】:

实际上,我不知道您为什么不能使用系统时间。如果您认为它可能不准确并且用户需要设置它,那当然可以通过代码来完成。以下 sn-ps 展示了如何从 C# 设置系统代码:

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime
{
    [MarshalAs(UnmanagedType.U2)]
    public short Year;
    [MarshalAs(UnmanagedType.U2)]
    public short Month;
    [MarshalAs(UnmanagedType.U2)]
    public short DayOfWeek;
    [MarshalAs(UnmanagedType.U2)]
    public short Day;
    [MarshalAs(UnmanagedType.U2)]
    public short Hour;
    [MarshalAs(UnmanagedType.U2)]
    public short Minute;
    [MarshalAs(UnmanagedType.U2)]
    public short Second;
    [MarshalAs(UnmanagedType.U2)]
    public short Milliseconds;
}
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetSystemTime(
[In] ref SystemTime lpSystemTime);

/// <summary>
/// Set the system time to the given date/time. 
/// The time must be given in utc.
/// </summary>
/// <param name="dt">Date/time to set the system clock to</param>
/// <returns>True on success, false on failure. This fails if the current user has insufficient rights to set the system clock. </returns>
/// <remarks>This method needs administrative priviledges to succeed.</remarks>
public static bool SetSystemTimeUtc(DateTime dt)
{
    bool bSuccess = false;
    if (dt.Year >= 2100) // limit the year to prevent older C-routines to crash on the local time
    {
        dt = new DateTime(2099, dt.Month, dt.Day);
    }

    SystemTime st = DateTimeToSystemTime(dt);
    try
    {
        bSuccess = SetSystemTime(ref st);
    }
    catch (System.UnauthorizedAccessException)
    {
        bSuccess = false;
    }
    catch (System.Security.SecurityException)
    {
        bSuccess = false;
    }

    return bSuccess;
}

private static SystemTime DateTimeToSystemTime(DateTime dt)
{
    SystemTime st;

    st.Year = (short)dt.Year;
    st.Day = (short)dt.Day;
    st.Month = (short)dt.Month;
    st.Hour = (short)dt.Hour;
    st.Minute = (short)dt.Minute;
    st.Second = (short)dt.Second;
    st.Milliseconds = (short)dt.Millisecond;
    st.DayOfWeek = (short)dt.DayOfWeek;

    return st;
}

如上注释所示,此方法需要用户是管理员,否则会失败。

【讨论】:

  • 我已经这样做了,但我不需要这个...用户可以保存试用版,所以他更改系统日期,所以我需要它
  • 啊...您想避免用户可以更改日期/时间设置以延长试用版的生命周期吗?那么坏消息是:如果没有外部资源,那基本上是不可能的。您可以尝试扫描磁盘以查找日期从不超过系统时间的文件,但这是一个漫长的过程并且可能不正确(也许系统时间已更正,因为它之前确实是错误的)。
【解决方案3】:

你已经知道答案了。做不到。

所以你需要改变你的策略。

如何判断是否有人作弊?

您需要创建一个可以用作参考的文件。试试这个,如果你需要的话,你可以让这个方法更难被发现,让用户更难克服。

当您安装程序时,创建一个文件,其内容是根据系统时间从某个随机日期开始的秒数。现在,一旦你运行你的程序,检查以确保时钟没有从那个时间倒退。如果有,问题!!!!,如果没有,再次更新文件。

这里的关键是向您认为会欺骗您的用户隐藏此文件。您可以将信息隐藏在注册表中,或作为不同文件的一部分。希望当用户意识到您正在存储他上次运行的时间时,他将无法弄清楚您如何存储上次运行的时间,并且无法向后设置标志。

这个系统似乎很容易被击败,但前提是最终用户知道您正在这样做,知道您存储信息的每个位置(将其存储在三个不同的文件中,包括 c:\temp),并且确切地知道如何您正在加密上次运行的日期。我不了解您的听众,但我敢打赌,您正试图阻止青少年永远使用试用版,一旦系统相当复杂(需要超过 5 分钟才能击败),您就会非常安全.

您可以做的另一件事是,当您检测到最终用户可能在作弊时,开始删除您的程序的某些部分,使其不再运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多