I can't get SetSystemTime to work in Windows Vista using C# with Interop (P/Invoke)

后端 未结 2 1196
孤城傲影
孤城傲影 2021-01-17 05:52

I\'m having a hard time getting SetSystemTime working in my C# code. SetSystemtime is a kernel32.dll function. I\'m using P/invoke (interop) to call it. SetSystemtime retu

相关标签:
2条回答
  • 2021-01-17 05:59

    You can't just add 3000 to the seconds field. You need to specify a number of seconds between 0 and 60. You need to adjust the seconds, minutes, hours, etc fields so that they are all within a valid range.

    Edit The simplest way would actually be to call SystemTimeToFileTime then add numSeconds * 10000 to the value it gives back, and then call FileTimeToSystemTime to convert back to a SystemTime.

    0 讨论(0)
  • 2021-01-17 06:07

    There is a .Net method to set the time; it's just not available in C# by default. If you add a reference to Microsoft.VisualBasic to your project then you can just write:

    Microsoft.VisualBasic.DateAndTime.TimeOfDay = DateTime.Now.AddSeconds(3000).AddMilliseconds(80);
    
    0 讨论(0)
提交回复
热议问题