小Q的博客

  • 首页
  • net编程
    • 产品和框架
    • 开发实例
    • 经验技巧
    • 开源组件
  • wp独立站
  • 自媒体
  • 日记本
  • 工具箱
每个程序员,都应该有一个自己的博客站
  1. 首页
  2. net编程
  3. 经验技巧
  4. 正文

net中c#教程 如何获取系统的环境变量?

2022年10月15日 1946点热度 0人点赞 0条评论

任何应用程序,都不是独立存在的,需要与外界交互。比如与使用者交互,收集各种数据;与数据库交互,读写业务逻辑;与操作系统交互,获取各种系统变量。今天分享的,就是如何读取操作系统的各种变量?

获取系统环境变量

Table of Contents

Toggle
  • 1、获取本机名称、Mac地址、登录用户
  • 2、获取各种系统路径
  • 3、内外网的IP地址
  • 4、打开文件和Url网址
  • 5、读取Ini文件
  • 6、读取DLL文件的版本信息
  • 7、反射创建实例
  • 8、反射获取类属性列表
  • 9、反射获取类属性值
  • 10、反射设置类属性值
  • 11、反射执行方法
  • 12、获取磁盘名称列表
  • 13、读取Config文件Key值

1、获取本机名称、Mac地址、登录用户

//1、获取本机名称、Mac地址、登录用户
string hostName = SystemHelper.GetHostName();           //DESKTOP-77MM5C2
string hostMac = SystemHelper.GetHostMac();             //54-27-1E-D9-4C-F7
string logonUser = SystemHelper.GetHostLogonUser();     //Administrator

 

2、获取各种系统路径

//2.1、获取各种系统路径(方法1)

//当前应用程序路径
//D:\NaviOther\tutorial\ConsoleApp1\bin\Debug
string appPath = Environment.CurrentDirectory;

//C:\Users\Administrator\AppData\Roaming
string folder1 = SystemHelper.GetSystemPath(SystemFolderName.AppData);

//C:\Users\Administrator\AppData\Local\Microsoft\Windows\INetCache
string folder2 = SystemHelper.GetSystemPath(SystemFolderName.Cache);

//C:\Users\Administrator\AppData\Local\Microsoft\Windows\INetCookies
string folder3 = SystemHelper.GetSystemPath(SystemFolderName.Cookies);

//C:\Users\Administrator\Desktop
string folder4 = SystemHelper.GetSystemPath(SystemFolderName.Desktop);

//C:\Users\Administrator\Favorites
string folder5 = SystemHelper.GetSystemPath(SystemFolderName.Favorites);

//C:\Windows\Fonts
string folder6 = SystemHelper.GetSystemPath(SystemFolderName.Fonts);

//C:\Users\Administrator\AppData\Local\Microsoft\Windows\History
string folder7 = SystemHelper.GetSystemPath(SystemFolderName.History);

//C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Network Shortcuts
string folder8 = SystemHelper.GetSystemPath(SystemFolderName.Nethood);

//C:\Users\Administrator\Documents
string folder9 = SystemHelper.GetSystemPath(SystemFolderName.Personal);

//C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
string folder10 = SystemHelper.GetSystemPath(SystemFolderName.PrintHood);

//C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
string folder11 = SystemHelper.GetSystemPath(SystemFolderName.Programs);

//C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Recent
string folder12 = SystemHelper.GetSystemPath(SystemFolderName.Recent);

//C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\SendTo
string folder13 = SystemHelper.GetSystemPath(SystemFolderName.SendTo);

//C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
string folder15 = SystemHelper.GetSystemPath(SystemFolderName.StartUp);

//C:\Windows\system32
string folder16 = SystemHelper.GetSystemPath(SystemFolderName.System32);

//C:\Users\Administrator\AppData\Local\Temp\
string folder17 = Path.GetTempPath();

//C:\Users\Administrator\AppData\Local\Temp\tmpE02F.tmp
string file1 = Path.GetTempFileName();

//2.2、获取各种系统路径(方法2)
//a,使用这个枚举:Environment.SpecialFolder
//b,再调用这个方法:Environment.GetFolderPath(xxx)
string folder18 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

 

3、内外网的IP地址

//3、内外网的IP地址
string inIP = SystemHelper.GetHostIP("192.168.0.");     //192.168.0.105
string outIP = SystemHelper.GetPublicIP();              //115.205.115.123

 

4、打开文件和Url网址

//4、打开文件和Url网址
SystemHelper.OpenFile("calc.exe", null);
SystemHelper.OpenUrl(@"https://www.navisoft.com.cn");

 

5、读取Ini文件

先定义一个Ini的文件

[settings]
angle_device=auto
skip_check_flag=0
gpu=auto
encoder=auto
decoder=auto

这里要调用系统API

//5、读取Ini文件
string iniSection = "settings";
string iniKey = "angle_device";
string iniFile = Environment.CurrentDirectory + @"\TestFile\test-ini.ini";
string iniKeyValue = SystemHelper.GetIniKeyValue(iniFile, iniSection, iniKey);
SystemHelper.SetIniKeyValue(iniFile, iniSection, iniKey, iniKeyValue + iniKeyValue);

 

6、读取DLL文件的版本信息

这里只能读取C#生成的DLL文件信息,系统或其他语言生成的,是读取失败的

//6、读取DLL文件的版本信息
string dllFile = @"D:\NaviOther\tutorial\tutorial.core\bin\Debug\Tutorial.Core.dll";
FileVersionInfo fvi = SystemHelper.GetDllVersionInfo(dllFile);
string fv1 = fvi.FileVersion;   //结果:1.0.0.0

 

7、反射创建实例

先创建一个测试类

namespace Tutorial.Core
{
    public class TestClass
    {
        public string Property1 { get; set; }
        public string Property2 { get; set; }


        public TestClass()
        {
            this.Property1 = "测试属性1";
        }

        public int InvokeMethod1(int int1, int int2)
        {
            int intR = int1 + int2;
            return intR;
        }
    }
}

 

//7、反射创建实例                                  
string instanceString = "Tutorial.Core.TestClass,Tutorial.Core";
object instance = SystemHelper.CreateInstance(instanceString);

 

8、反射获取类属性列表

//8、反射获取类属性列表
List<string> listPi = SystemHelper.GetPropertyNames(instance);

 

9、反射获取类属性值

//9、反射获取类属性值
PropertyInfo pi1 = SystemHelper.GetPropertyInfo(instance, "Property1");
object piValue1 = SystemHelper.GetPropertyValue(instance, "Property1");     //结果:测试属性1

 

10、反射设置类属性值

//10、反射设置类属性值
SystemHelper.SetPropertyValue(instance, "Property1", "111111");
object piValue2 = SystemHelper.GetPropertyValue(instance, "Property1");     //结果:111111

 

11、反射执行方法

//11、反射执行类方法
List<object> listP = new List<object>() { 11, 12 };
object objR = SystemHelper.ExecuteMethod(instance, "InvokeMethod1", listP); //结果:23

 

12、获取磁盘名称列表

//12、获取磁盘名称列表
string[] listDrive = Environment.GetLogicalDrives();

 

13、读取Config文件Key值

先定义一个Config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <!--产品编码、名称和标题-->
        <add key="ProductCode" value="abcd64a1-9f08-4ad3-a304-3450730d39ca"/>
        <add key="ProductName" value="Tutorial"/>
        <add key="ProductTitle" value="NaviSoft软件教程"/>
    </appSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
    </startup>
</configuration>

再调用代码读取

//13、读取Config文件Key值
string configFile = Environment.CurrentDirectory + @"\ZConsoleApp1.exe.config";
string sectionName = "appSettings";
string keyValue = "ProductName";

//结果都是:Tutorial
string productName1 = SystemHelper.GetConfigValue(configFile, sectionName, keyValue);
string productName2 = SystemHelper.GetAppSettingsValue(keyValue);

 

相关阅读

net中c#教程 如何递归获取文件夹所有文件?

net中c#教程 file文件的常用操作

net中c#教程 Json字符串的常用操作

net中c#教程 DataTable的常用操作

net中c#教程 DateTime日期时间的常用操作

net中c#教程 list泛型的常用操作

net中c#教程 string字符串的常用操作

标签: c#教程 net教程 反射调用 环境变量 读取config文件 读取ini文件
最后更新:2022年10月15日

小Q

80后中年不油腻大叔,喜欢编写代码、打羽毛球、做木制玩具。目前定居浙江杭州

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 小Q的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备2022019157号-2