当前位置:网站首页>ini怎么使用? C#教程
ini怎么使用? C#教程
2022-08-04 21:02:00 【cfqq1989】
在硬盘路径下保存数据。
结构体其中的一个值,或对象的某个属性值,保存在硬盘。
新建对象后,再把值一一赋值给对象。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.IO;
namespace Help
{
public class Help_Ini
{
#region API函数声明 (应用编程接口)
//using System.Runtime.InteropServices;
[DllImport("kernel32")]//返回0表示失败,非0为成功 (比如调用C++ 非托管类型的DLL)
private static extern long WritePrivateProfileString(string section, string key,
string val, string filePath);
[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section, string key,
string def, StringBuilder retVal, int size, string filePath);
#endregion
/// <summary>
/// get读
/// private string path = Application.StartupPath + "\\Config\\Port.ini";
/// 路径如"C:\\Users\\Administrator\\Desktop\\ScannerProj\\ScannerProj\\ScannerProj\\bin\\Debug\\Config\\Port.ini"
/// </summary>
/// <param name="Section">标签</param>
/// <param name="Key">属性</param>
/// <param name="NoText">值</param>
/// <param name="iniFilePath">硬盘路径</param>
/// <returns>string</returns>
public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
{//using System.IO;
if (File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
return temp.ToString();
}
else
{
return string.Empty;
}
}
/// <summary>
/// set写
/// </summary>
/// <param name="Section">标签</param>
/// <param name="Key">属性</param>
/// <param name="Value">值</param>
/// <param name="iniFilePath">硬盘路径</param>
/// <returns>bool</returns>
public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
{
if (File.Exists(iniFilePath))
{
return WritePrivateProfileString(Section, Key, Value, iniFilePath) != 0;
}
return false;
}
}
}
边栏推荐
猜你喜欢
随机推荐
【2022杭电多校5 1012题 Buy Figurines】STL的运用
Big capital has begun to flee the crypto space?
括号匹配
后缀式的计算
暴雨中的人
dotnet 启动 JIT 多核心编译提升启动性能
顺序队列
某男子因用本地虚拟机做压测,惨遭字节面试官当场嘲笑
路由中的meta、params传参的一些问题(可传不可传,为空,搭配,点击传递多次参数报错)
模拟对抗之红队免杀开发实践
Using Baidu EasyDL to realize forest fire early warning and identification
三种方式设置特定设备UWP XAML view
【C语言】指针和数组的深入理解(第三期)
深度解析:为什么跨链桥又双叒出事了?
无代码平台字段设置:基础设置入门教程
LINQ to SQL (Group By/Having/Count/Sum/Min/Max/Avg操作符)
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning Code Analysis
【1403. 非递增顺序的最小子序列】
3、IO流之字节流和字符流
结构体小结



![[2022 Nioke Duo School 5 A Question Don't Starve] DP](/img/fa/f1d11297cc5f58919bcc579f0a82e9.png)





