C# Ini配置文件
2024-10-18 19:27:00
public class INIUserAccound
{ static IniFile Ini = new IniFile(AppDomain.CurrentDomain.BaseDirectory + @"\Config\User.Ini"); const string Session = "UserInfo";
/// <summary>
/// 获得用户名
/// </summary>
/// <returns></returns>
public static string GetAccound()
{
return Ini.IniReadValue(Session, "Accound");
}
/// <summary>
/// 保存用户名
/// </summary>
/// <param name="SkinName"></param>
public static void SetAccound(string Accound)
{
Ini.IniWriteValue(Session, "Accound", Accound);
} /// <summary>
/// 获得密码
/// </summary>
/// <returns></returns>
public static string GetPwd()
{
return Ini.IniReadValue(Session, "Pwd");
}
/// <summary>
/// 保存密码
/// </summary>
/// <param name="SkinName"></param>
public static void SetPwd(string Pwd)
{
Ini.IniWriteValue(Session, "Pwd", Pwd);
} }
/// <summary>
/// 操作INI文件类 测试信息
/// </summary>
public class IniFile
{
const int DATA_SIZE = 1024; private string _path; //INI档 案 名
public string IniPath { get { return _path; } set { _path = value; } } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = DATA_SIZE)]
public string szText;
} //读写INI文件的API函数
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern int GetPrivateProfileString(string section, string key, string def, out STRINGBUFFER retVal, int size, string filePath); //类的构造函数,传递INI档案名
public IniFile(string sPath)
{
_path = sPath;
string path = IniPath.Substring(0, IniPath.LastIndexOf("\\"));
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
if (!File.Exists(_path)) CreateIniFile();
} //写INI文件
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this._path);
} //读取INI文件指定项目的数据
public string IniReadValue(string Section, string Key)
{
int i;
STRINGBUFFER RetVal;
i = GetPrivateProfileString(Section, Key, null, out RetVal, DATA_SIZE, this._path);
string temp = RetVal.szText;
return temp.Trim();
} //读取INI文件指定项目的数据
public string IniReadValue(string Section, string Key, string defaultValue)
{
int i;
STRINGBUFFER RetVal;
i = GetPrivateProfileString(Section, Key, null, out RetVal, DATA_SIZE, this._path);
string temp = RetVal.szText;
return temp.Trim() == "" ? defaultValue : temp.Trim();
} /// <summary>
/// 创建INI文件
/// </summary>
public void CreateIniFile()
{
StreamWriter w = File.CreateText(_path);
w.Write("");
w.Flush();
w.Close();
}
}
最新文章
- Membership三步曲之进阶篇 - 深入剖析Provider Model
- JS 中 Array.slice() 和 Array.splice()方法
- sqlserver临时表操作
- ubuntu删除软件命令
- Linux删除文件夹命令
- Telnet连接Win7系统被拒绝的原因及解决方法
- git 添加忽略文件
- [置顶] android 自定义圆角ImageView以及锯齿的处理
- 57. Spring 自定义properties升级篇【从零开始学Spring Boot】
- C#开发学习——web服务器端控件
- Linux 性能搜集【linux_reports-cpu/memory/disks/network】
- [2017-7-28]Android Learning Day7
- loadrunner笔记(三):设置、运行场景和生成测试报告
- 在 Linux 上使用 VirtualBox 的命令行管理界面
- (转)每天一个linux命令(21):find命令之xargs
- angular指令中使用ngModelController
- [C++ Primer] 第3章: 字符串, 向量和数组
- Android 编辑框插入表情图片
- ios retainCount
- 改变Fragment的默认动画
热门文章
- SQLServer出现 &#39;其他会话正在使用事务的上下文&#39; 的问题原因,什么是环回链接服务器?(转载)
- 四种MySQL存储引擎
- (顺序表的应用5.4.2)POJ 1591 M*A*S*H(约瑟夫环问题的变形——变换步长值)
- android 项目学习随笔五(JSON解析)
- codereview
- Apache服务器访问过慢分析及解决
- Atom Remote-FTP connecting FTP with SSL/TLS
- 时间戳转换成Date
- php number_format()保留小数点后几位
- linux命令:du 命令