C # uses the Win32 Api to modify the local system time, obtain the hard drive serial number, can be used to write software registration mechanism!
The code is as follows:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.InteropServices;
Namespace Fengyun
{
public class Win32
{
#region Modifying the Local system time
[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>
Synchronizing local time with SQL Server time
</summary>
<param name= "Sqlservertime" > Time </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 get the hard drive serial number
[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>
Get the hard drive serial number
</summary>
<param name= "Drvid" > Hard drive letter [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
}
}
The above is the entire content of the code that this article shares, hope to learn C # can help you.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # leverages the Win32 Api to modify the local system time, get the hard drive serial number
This address: http://www.paobuke.com/develop/c-develop/pbk23115.html
Related Content C # FileStream read and write text manipulation code Analysis LZW compression algorithm C # source ASP. generate static pages for batch and single page generation detailed introduction to the development and problems of C # 's File Verification tool
C # The method of implementing Chinese characters to pinyin or Pinyin first letter C # non-rectangular form implementation Method C # WinForm WebBrowser Practical Tips Summary of the difference between strings and string in C #
C # leverages the Win32 Api to modify the local system time, get the hard drive serial number