C # leverages the Win32 Api to modify the local system time, get the hard drive serial number

Source: Internet
Author: User
Tags local time

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.