C# .NET常用操作

文章介绍了如何在C#中通过WindowsManagementInstrumentation(WMI)获取设备的序列号和MAC地址,同时提供了本地文件的读写操作,包括写入文本文件、写入JSON文件、读取文件和删除文件的方法。
摘要由CSDN通过智能技术生成

获取设备序列号和mac,以及本地文件读写

/ 设备唯一id获取
        public static class DeviceUniqueId
        {
            public static string GetHardDiskSerialNumber()
            {
                try
                {
                    ManagementClass managementClass = new ManagementClass("Win32_DiskDrive");
                    ManagementObjectCollection managementObjects = managementClass.GetInstances();

                    foreach (ManagementObject managementObject in managementObjects)
                    {
                        if (managementObject.Properties["SerialNumber"].Value != null)
                        {
                            return managementObject.Properties["SerialNumber"].Value.ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                return string.Empty;
            }

            public static string GetMacAddress()
            {
                try
                {
                    ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection managementObjects = managementClass.GetInstances();

                    foreach (ManagementObject managementObject in managementObjects)
                    {
                        if ((bool)managementObject["IPEnabled"])
                        {
                            return managementObject["MacAddress"].ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                return string.Empty;
            }
        }
        // 读写操作
        class FileHelper
        {
            public static bool WriteToFile(string fileName, object content)
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
                string serializedContent = content.GetType().IsValueType || content.GetType() == typeof(string)
                    ? content.ToString()
                    : JsonConvert.SerializeObject(content);

                try
                {
                    File.WriteAllText(filePath, serializedContent, Encoding.UTF8);
                    Console.WriteLine("写入文件成功:" + filePath);
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("写入文件失败:" + ex.Message);
                    return false;
                }
            }

            public static bool WriteToFileJson(string fileName, object content)
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
                string serializedContent = JsonConvert.SerializeObject(content);

                try
                {
                    File.WriteAllText(filePath, serializedContent, Encoding.UTF8);
                    Console.WriteLine("写入文件成功:" + filePath);
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("写入文件失败:" + ex.Message);
                    return false;
                }
            }

            public static string ReadFile(string fileName)
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

                try
                {
                    string content = File.ReadAllText(filePath, Encoding.UTF8);
                    Console.WriteLine("读取文件成功:" + filePath);
                    return content;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("读取文件失败:" + ex.Message);
                    return null;
                }
            }

            public static bool DeleteFile(string fileName)
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

                try
                {
                    File.Delete(filePath);
                    Console.WriteLine("删除文件成功:" + filePath);
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("删除文件失败:" + ex.Message);
                    return false;
                }
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值