当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢
随机推荐
三种方式设置特定设备UWP XAML view
jekyll adds a flowchart to the blog
LINQ to SQL (Group By/Having/Count/Sum/Min/Max/Avg操作符)
LayaBox---TypeScript---结构
1.读写点云文件
STP基本配置及802.1D生成树协议的改进
【2022牛客多校5 A题 Don‘t Starve】DP
dotnet 删除只读文件
adb shell input keyevent 模拟按键事件
暴雨中的人
[2022 Hangzhou Electric Power Multi-School 5 1012 Questions Buy Figurines] Application of STL
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
明明加了唯一索引,为什么还是产生了重复数据?
数字IC设计中基本运算的粗略的延时估计
88.(cesium之家)cesium聚合图
【PCBA方案设计】握力计方案
PowerCLi 导入License到vCenter 7
How to make good use of builder mode
[2022 Nioke Duo School 5 A Question Don't Starve] DP
Comic | Two weeks after the boss laid me off, he hired me back and doubled my salary!