今天在寫一個程式抓取系統時間後轉成BYTE[],然後再轉回SYSTEMTIME去修改電腦時間。
去MSDN上看如何修改系統時間網址如下
http://msdn.microsoft.com/zh-tw/library/ms172517(VS.80).aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace test
{
class Program
{
[Serializable]
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;
}
[DllImport("kernel32.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("kernel32.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
static void Main(string[] args)
{
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME systime1 = new SYSTEMTIME();
GetSystemTime(ref systime1);
byte[] b1 = Serialize(systime1);
SYSTEMTIME systime2 = new SYSTEMTIME();
GetSystemTime(ref systime2);
systime2 = (SYSTEMTIME)Deserialize(b1);
// Set the system clock ahead one hour.
systime2.wHour =( ushort)(systime2.wHour + 1%24);
// systime2.wHour = 3;
MessageBox.Show("New time: " + systime2.wHour.ToString() + ":"
+ systime2.wMinute.ToString());
SetSystemTime(ref systime2);
}
///<summary>
/// 序列化
/// </summary>
/// <param name="data">要序列化的对象</param>
/// <returns>返回存放序列化后的数据缓冲区</returns>
public static byte[] Serialize(object data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream();
formatter.Serialize(rems, data);
return rems.GetBuffer();
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="data">数据缓冲区</param>
/// <returns>对象</returns>
public static object Deserialize(byte[] data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream(data);
data = null;
return formatter.Deserialize(rems);
}
}
}
結果成事執行可以成功但時間不會改
本來以為是自己問題結果用管理員權限執行就可以更改了
去MSDN上看如何修改系統時間網址如下
http://msdn.microsoft.com/zh-tw/library/ms172517(VS.80).aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace test
{
class Program
{
[Serializable]
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;
}
[DllImport("kernel32.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("kernel32.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
static void Main(string[] args)
{
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME systime1 = new SYSTEMTIME();
GetSystemTime(ref systime1);
byte[] b1 = Serialize(systime1);
SYSTEMTIME systime2 = new SYSTEMTIME();
GetSystemTime(ref systime2);
systime2 = (SYSTEMTIME)Deserialize(b1);
// Set the system clock ahead one hour.
systime2.wHour =( ushort)(systime2.wHour + 1%24);
// systime2.wHour = 3;
MessageBox.Show("New time: " + systime2.wHour.ToString() + ":"
+ systime2.wMinute.ToString());
SetSystemTime(ref systime2);
}
///<summary>
/// 序列化
/// </summary>
/// <param name="data">要序列化的对象</param>
/// <returns>返回存放序列化后的数据缓冲区</returns>
public static byte[] Serialize(object data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream();
formatter.Serialize(rems, data);
return rems.GetBuffer();
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="data">数据缓冲区</param>
/// <returns>对象</returns>
public static object Deserialize(byte[] data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream(data);
data = null;
return formatter.Deserialize(rems);
}
}
}
結果成事執行可以成功但時間不會改
本來以為是自己問題結果用管理員權限執行就可以更改了
全站熱搜
留言列表