C#利用win32 Api 修改本地系统时间、获取硬盘序列号

Thadea ·
更新时间:2024-11-14
· 654 次阅读

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# web api返回类型设置为json的两种方法C#中通过API实现的打印类 实例代码C#实现快递api接口调用方法C#通过WIN32 API实现嵌入程序窗体使用C#调用系统API实现内存注入的代码ASP.NET(C#) Web Api通过文件流下载文件的实例c#调用api控制windows关机示例(可以重启/注销)C#获取USB事件API实例分析c#之利用API函数实现动画窗体的方法详解C# API中模型与它们的接口设计详解



系统时间 C# win32 系统 序列号 win 硬盘 api

需要 登录 后方可回复, 如果你还没有账号请 注册新账号