c#修改本机时间,获取硬盘序列号
2010-01-22 16:35
302 查看
代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Fengyun
{
public class Win32
{
#region 修改本地系统时间
[DllImport("Kernel32.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("Kernel32.dll")]
private extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);
[StructLayout(LayoutKind.Sequential)]
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;
}
/// <summary>
/// 将本地时间与sqlserver服务器时间同步
/// </summary>
/// <param name="SqlServerTime">时间</param>
public static void SetTime(DateTime SqlServerTime)
{
SYSTEMTIME st = new SYSTEMTIME();
st.wYear = Convert.ToUInt16(SqlServerTime.Year);
st.wMonth = Convert.ToUInt16(SqlServerTime.Month);
st.wDay = Convert.ToUInt16(SqlServerTime.Day);
st.wHour = Convert.ToUInt16(SqlServerTime.Hour);
st.wMilliseconds = Convert.ToUInt16(SqlServerTime.Millisecond);
st.wMinute = Convert.ToUInt16(SqlServerTime.Minute);
st.wSecond = Convert.ToUInt16(SqlServerTime.Second);
SetLocalTime(ref st);
}
#endregion
#region 获取硬盘序列号
[DllImport("kernel32.dll")]
private static extern int GetVolumeInformation(
string lpRootPathName,
string lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
int lpMaximumComponentLength,
int lpFileSystemFlags,
string lpFileSystemNameBuffer,
int nFileSystemNameSize
);
/// <summary>
/// 获取硬盘序列号
/// </summary>
/// <param name="drvID">硬盘盘符[c|d|e|....]</param>
/// <returns></returns>
public static string GetDiskVolume(string drvID)
{
const int MAX_FILENAME_LEN = 256;
int retVal = 0;
int lpMaximumComponentLength = 0;
int lpFileSystemFlags = 0;
string lpVolumeNameBuffer = null;
string lpFileSystemNameBuffer = null;
int i = GetVolumeInformation(
drvID + @":\",
lpVolumeNameBuffer,
MAX_FILENAME_LEN,
ref retVal,
lpMaximumComponentLength,
lpFileSystemFlags,
lpFileSystemNameBuffer,
MAX_FILENAME_LEN
);
return retVal.ToString("x");
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Fengyun
{
public class Win32
{
#region 修改本地系统时间
[DllImport("Kernel32.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("Kernel32.dll")]
private extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);
[StructLayout(LayoutKind.Sequential)]
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;
}
/// <summary>
/// 将本地时间与sqlserver服务器时间同步
/// </summary>
/// <param name="SqlServerTime">时间</param>
public static void SetTime(DateTime SqlServerTime)
{
SYSTEMTIME st = new SYSTEMTIME();
st.wYear = Convert.ToUInt16(SqlServerTime.Year);
st.wMonth = Convert.ToUInt16(SqlServerTime.Month);
st.wDay = Convert.ToUInt16(SqlServerTime.Day);
st.wHour = Convert.ToUInt16(SqlServerTime.Hour);
st.wMilliseconds = Convert.ToUInt16(SqlServerTime.Millisecond);
st.wMinute = Convert.ToUInt16(SqlServerTime.Minute);
st.wSecond = Convert.ToUInt16(SqlServerTime.Second);
SetLocalTime(ref st);
}
#endregion
#region 获取硬盘序列号
[DllImport("kernel32.dll")]
private static extern int GetVolumeInformation(
string lpRootPathName,
string lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
int lpMaximumComponentLength,
int lpFileSystemFlags,
string lpFileSystemNameBuffer,
int nFileSystemNameSize
);
/// <summary>
/// 获取硬盘序列号
/// </summary>
/// <param name="drvID">硬盘盘符[c|d|e|....]</param>
/// <returns></returns>
public static string GetDiskVolume(string drvID)
{
const int MAX_FILENAME_LEN = 256;
int retVal = 0;
int lpMaximumComponentLength = 0;
int lpFileSystemFlags = 0;
string lpVolumeNameBuffer = null;
string lpFileSystemNameBuffer = null;
int i = GetVolumeInformation(
drvID + @":\",
lpVolumeNameBuffer,
MAX_FILENAME_LEN,
ref retVal,
lpMaximumComponentLength,
lpFileSystemFlags,
lpFileSystemNameBuffer,
MAX_FILENAME_LEN
);
return retVal.ToString("x");
}
#endregion
}
}
相关文章推荐
- C#利用win32 Api 修改本地系统时间、获取硬盘序列号
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- 使用C#获取CPU及硬盘序列号的源代码
- C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型
- C#获取ftp文件最后修改时间
- C#获取cpu序列号,硬盘ID,网卡MAC地址
- c#获取MAC地址 硬盘序列号
- C# 获取CPU序列号、网卡MAC地址、硬盘序列号封装类,用于软件绑定电脑
- C#应用:获取cpu序列号,硬盘ID,网卡MAC地址[编程联盟]
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- C# 获取硬盘序列号 Volume Serial Number
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存 【转载】
- c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存
- 使用C#获取CPU及硬盘序列号的源代码
- .net C# 获取硬盘号 CPU序列号 IP地址 Mac地址
- 【转载】使用C#获取CPU及硬盘序列号的源代码
- C#获取cpu序列号,硬盘ID,网卡MAC地址
- C# 获取电脑MAC地址,IP地址,物理内存,CPU序列号,硬盘ID..........................
- C#获取硬盘序列号,CPU序列号,网卡MAC地址
- C#获取CPU序列号、网卡MAC地址、硬盘序列号封装类