C#利用win32 Api 修改本地系统时间、获取硬盘序列号
程序员文章站
2023-12-13 10:58:22
c#利用win32 api 修改本地系统时间、获取硬盘序列号,可以用于软件注册机制的编写!
复制代码 代码如下:
using system;
using system...
c#利用win32 api 修改本地系统时间、获取硬盘序列号,可以用于软件注册机制的编写!
复制代码 代码如下:
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#能有所帮助。