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

 更新时间:2015年03月11日 16:08:23   投稿:hebedich  
这篇文章主要介绍了C#利用win32 Api 修改本地系统时间、获取硬盘序列号的方法及代码分享,需要的朋友可以参考下

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#中遍历DataSet数据集对象实例

    C#中遍历DataSet数据集对象实例

    这篇文章主要介绍了C#中遍历DataSet数据集对象实例,经常忘记如何操作DataSet,这里记下来并分享,让需要的朋友可以参考下
    2014-08-08
  • C#中struct和class的区别详解

    C#中struct和class的区别详解

    这篇文章主要介绍了C#中struct和class的区别,对C#初学者来说是需要牢固掌握的,需要的朋友可以参考下
    2014-08-08
  • 浅谈C#多线程简单例子讲解

    浅谈C#多线程简单例子讲解

    本篇文章主要介绍了C#多线程简单例子,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C#实现程序开机启动的方法

    C#实现程序开机启动的方法

    这篇文章主要介绍了C#实现程序开机启动的方法,涉及C#针对应用程序及注册表的相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • C#中的Action、Func和Predicate如何使用

    C#中的Action、Func和Predicate如何使用

    这篇文章主要给大家介绍了关于C#中Action、Func和Predicate如何使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • C#实现绘制面形图表的方法详解

    C#实现绘制面形图表的方法详解

    这篇文章主要介绍了C#实现绘制面形图表的方法,对于C#初学者很好的掌握C#图形绘制有一定的借鉴价值,需要的朋友可以参考下
    2014-07-07
  • c#  Linq distinct不会调用Equals方法详解

    c# Linq distinct不会调用Equals方法详解

    这篇文章主要介绍了c# Linq distinct不会调用Equals方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 比较有效的使用C#读取文件的代码

    比较有效的使用C#读取文件的代码

    比较有效的使用C#读取文件的代码...
    2007-08-08
  • 3种C# 加载Word的方法

    3种C# 加载Word的方法

    本次经验内容分享通过C#程序来加载Word文档的3种不同方法。分别是:加载本地Word文档、以只读模式加载Word文档、从流加载Word 想具体了解的小伙伴请参考下文
    2021-09-09
  • C#网络爬虫代码分享 C#简单的爬取工具

    C#网络爬虫代码分享 C#简单的爬取工具

    这篇文章主要为大家详细介绍了C#网络爬虫代码,教大家如何制作了简单的爬取工具,感兴趣的小伙伴们可以参考一下
    2016-07-07

最新评论