C# ini (配置文件)API调用以及例子
首先是INI 类例子里面包含了INI用到的API的声明以及 读写两个方法:
1 public class Ini//文件读写 2 { 3 // 声明INI文件的写操作函数 WritePrivateProfileString() 4 5 [System.Runtime.InteropServices.DllImport("kernel32")] 6 7 private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); 8 9 // 声明INI文件的读操作函数 GetPrivateProfileString() 10 11 [System.Runtime.InteropServices.DllImport("kernel32")] 12 13 private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath); 14 15 16 private string sPath = null; 17 public Ini(string path) 18 { 19 this.sPath = path; 20 } 21 22 public void Writue(string section, string key, string value) 23 { 24 25 // section=配置节,key=键名,value=键值,path=路径 26 27 WritePrivateProfileString(section, key, value, sPath); 28 29 } 30 public string ReadValue(string section, string key) 31 { 32 33 // 每次从ini中读取多少字节 34 35 System.Text.StringBuilder temp = new System.Text.StringBuilder(255); 36 37 // section=配置节,key=键名,temp=上面,path=路径 38 39 GetPrivateProfileString(section, key, "", temp, 255, sPath); 40 41 return temp.ToString(); 42 43 } 44 }
然后是方法调用例子:
写入:
string Current;
Current = Directory.GetCurrentDirectory();//获取当前根目录
Ini ini = new Ini(Current + "/config.ini");
ini.Writue(string 标签,string 识别键, value 保存参数);
读取:
string Current;
Current = Directory.GetCurrentDirectory();//获取当前根目录
Ini ini = new Ini(Current + "/config.ini");
string a = ini.Readvalue(string 标签,string 识别键);
分类:
C#基础学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗