国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#利用win32 Api 修改本地系統時間、獲取硬盤序列號

2020-01-24 02:08:01
字體:
來源:轉載
供稿:網友

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#能有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石河子市| 博罗县| 元氏县| 石家庄市| 盈江县| 铅山县| 定兴县| 化州市| 宁都县| 广德县| 汽车| 分宜县| 保靖县| 达日县| 图们市| 鹰潭市| 左云县| 镇原县| 荔波县| 邵东县| 齐齐哈尔市| 上栗县| 柳州市| 安乡县| 康保县| 于都县| 南丹县| 柳河县| 大庆市| 靖江市| 二手房| 吉隆县| 日土县| 延津县| 甘洛县| 晋中市| 寿宁县| 眉山市| 嘉黎县| 中西区| 巧家县|