C#利用win32 Api 修改本地系统时间、获取硬盘序列号
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#能有所帮助。
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
深入了解c# 匿名类型
一、什么叫做匿名类? 匿名类就是没有名字的类。匿名类不能被引用,只能再创建的时候用new语句来声明。二、匿名类的优势以及应用场景; 1、匿名类型提供了一种方...
[阅读全文]
-
-
C# 实现俄罗斯方块(附源码)
概述俄罗斯方块(tetris)是一款由俄罗斯人阿列克谢·帕基特诺夫发明的休闲游戏,帕基特诺夫爱玩拼图,从拼图游戏里得到灵感,设计出了俄罗斯方块。由于上手简单、老...
[阅读全文]
-
浅析c# 接口
接口:是指定一组函数成员而不是实现他们的引用类型。所以只能类喝啊结构来实现接口,在结成该接口的类里面必须要实现接口的所有方法接口的特点:继承于接口的类,必须要实...
[阅读全文]
-
c# 接口使用实例
用接口实现一个简单的物件的入库,出库如定义一个物流类接口,包含物件所属快递公司名称属性,物件单号属性及信息显示方法。通过物件出库类信息和物件入库类信息继承该接口...
[阅读全文]
-
-
-
-
-
快速了解c# 常量
常量是固定值,程序执行期间不会改变。常量可以是任何基本数据类型,比如整数常量、浮点常量、字符常量或者字符串常量,还有枚举常量。常量可以被当作常规的变量,只是它们...
[阅读全文]
网友评论