How to change system date and time with vb.net on windows 7

爱⌒轻易说出口 提交于 2019-12-02 10:27:57

By far the easiest way to do this is to use a built in method in the Microsoft.VisualBasic namespace:

    'create a date to use
    Dim d As DateTime
    'set the date to a hour from now
    d = DateTime.Now.AddHours(1)
    'set the system date and time to this date and time - throw an exception if we can't set it
    Try
        Microsoft.VisualBasic.TimeOfDay = d
    Catch ex As Exception
        MessageBox.Show("Could not set the time. You probably need to run as Administrator to do this. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
    End Try

Note that you usually need to be running as admin in order to change the system date or time.

You might not have the privilege to do so...

The calling process must have the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default. The SetLocalTime function enables the SE_SYSTEMTIME_NAME privilege before changing the local time and disables the privilege before returning.

More on this here...

http://msdn.microsoft.com/en-us/library/windows/desktop/ms717802(v=vs.85).aspx

Pham Thanh

Simply use this code

Today = "06/09/2015 06:09:06 PM"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!