C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤(pán)序列號(hào)
C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤(pán)序列號(hào),可以用于軟件注冊(cè)機(jī)制的編寫(xiě)!
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Fengyun
{
public class Win32
{
#region 修改本地系統(tǒng)時(shí)間
[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>
/// 將本地時(shí)間與sqlserver服務(wù)器時(shí)間同步
/// </summary>
/// <param name="SqlServerTime">時(shí)間</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 獲取硬盤(pán)序列號(hào)
[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>
/// 獲取硬盤(pán)序列號(hào)
/// </summary>
/// <param name="drvID">硬盤(pán)盤(pán)符[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
}
}
以上就是本文所分享的代碼的全部?jī)?nèi)容了,希望對(duì)大家學(xué)習(xí)C#能有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12基于C#實(shí)現(xiàn)語(yǔ)音識(shí)別功能詳解
在.NET4.0中,可以借助System.Speech組件讓電腦來(lái)識(shí)別我們的聲音。本文將利用該組件實(shí)現(xiàn)語(yǔ)音識(shí)別功能,文中實(shí)現(xiàn)過(guò)程講解詳細(xì),需要的可以參考一下2022-04-04C#基礎(chǔ)知識(shí)之base關(guān)鍵字介紹
本文主要介紹base關(guān)鍵字的使用方法,base關(guān)鍵字可以調(diào)用基類重寫(xiě)的方法,可以調(diào)用基類的構(gòu)造方法,還可以在EntityFramework中使用,下面一一介紹。2016-04-04C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則的示例詳解
在面向?qū)ο缶幊讨?,SOLID?是五個(gè)設(shè)計(jì)原則的首字母縮寫(xiě),旨在使軟件設(shè)計(jì)更易于理解、靈活和可維護(hù)。本文將通過(guò)實(shí)例詳細(xì)講講C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則,需要的可以參考一下2022-07-07C#實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證的方法
這篇文章主要介紹了C#實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證的方法,通過(guò)封裝的類文件實(shí)例化調(diào)用實(shí)現(xiàn)了對(duì)身份證號(hào)碼的驗(yàn)證,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11