C# 操作.ini文件
1.声明变量
#region "声明变量" /// <summary>
/// 写入INI文件
/// </summary>
/// <param name="section">节点名称[如[TypeName]]</param>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">节点名称</param>
/// <param name="key">键</param>
/// <param name="def">值</param>
/// <param name="retval">stringbulider对象</param>
/// <param name="size">字节大小</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); private string strFilePath = Application.StartupPath + "\\FileConfig.ini";//获取INI文件路径
private string strSec = ""; //INI文件名 #endregion
2.写入到ini文件
private void WriteIni()
{
try
{
//根据INI文件名设置要写入INI文件的节点名称
//此处的节点名称完全可以根据实际需要进行配置
strSec = Path.GetFileNameWithoutExtension(strFilePath);
WritePrivateProfileString(strSec, "UserName", EDncrypt.MD5Encrypt(txtUser.Text.Trim()), strFilePath);
WritePrivateProfileString(strSec, "Password", EDncrypt.MD5Encrypt(txtPass.Text.Trim()), strFilePath);
WritePrivateProfileString(strSec, "DataBase", EDncrypt.MD5Encrypt(txtDataBase.Text.Trim()), strFilePath);
WritePrivateProfileString(strSec, "Host", EDncrypt.MD5Encrypt(txtServer.Text.Trim()), strFilePath);
MessageBox.Show("写入成功"); }
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
3.读取ini文件
private void ReadIni()
{
if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
{
strSec = Path.GetFileNameWithoutExtension(strFilePath);
txtUser.Text = ContentValue(strSec, "UserName");
txtPass.Text = ContentValue(strSec, "Password");
txtDataBase.Text = ContentValue(strSec, "DataBase");
txtServer.Text = ContentValue(strSec, "Host");
}
else
{
MessageBox.Show("INI文件不存在");
}
}
/// <summary>
/// 自定义读取INI文件中的内容方法
/// </summary>
/// <param name="Section">键</param>
/// <param name="key">值</param>
/// <returns></returns>
private string ContentValue(string Section, string key)
{
StringBuilder temp = new StringBuilder();
GetPrivateProfileString(Section, key, "", temp, , strFilePath);
return EDncrypt.MD5Decrypt(temp.ToString());
}
4.Md5加密与解密类
public class EDncrypt
{
private static readonly string m_strKey = "dre34ASD";
///MD5加密
public static string MD5Encrypt(string pToEncrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(m_strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(m_strKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
} ///MD5解密
public static string MD5Decrypt(string pToDecrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / ];
for (int x = ; x < pToDecrypt.Length / ; x++)
{
int i = (System.Convert.ToInt32(pToDecrypt.Substring(x * , ), ));
inputByteArray[x] = (byte)i;
} des.Key = ASCIIEncoding.ASCII.GetBytes(m_strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(m_strKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
}
C# 操作.ini文件的更多相关文章
- 关于C#操作INI文件的总结
原文:关于C#操作INI文件的总结 INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下: [Section1]key 1 = value2key 1 = value2--[S ...
- C#利用Vini.cs操作INI文件
VClassLib-CS项目Github地址:https://github.com/velscode/VClassLib-CS VINI文档地址:https://github.com/velscode ...
- [转]C#操作INI文件
在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据库连接,及保存密码设 ...
- QSettings配置读写-win注册表操作-ini文件读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写 本文地址:http:// ...
- C#操作INI文件(明天陪你看海)
C#操作INI文件 在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据 ...
- 【转】操作ini文件
一.INI文件的结构: ; 注释 [小节名] 关键字=值 INI文件有多个小节,每个小节又有多个关键字, “=”后面是该关键字的值. 值的类型有三种:字符串.整型数值和布尔值. 其中字符串存贮在IN ...
- .net操作InI文件
public class INI { public static string IniFileName = "";//路径 [DllImport("kernel32&qu ...
- Delphi操作Ini文件
Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件 关键字1=值1 关键子2=值2INI文件允许有多个小节, ...
- .net 操作INI文件
using System.Runtime.InteropServices; using System.Text; namespace FaureciaManager { public class Fi ...
- python操作ini文件
简介 ini文件作为常见的配置文件,因此需要对ini文件做处理,此处使用configparser模块,本文介绍以下ini文件常用的处理方式. 需要读取的ini文件 如下文件,[ ]包含的称为secti ...
随机推荐
- UIImage图片处理,旋转、截取、平铺、缩放等操作
来源:iOS_小松哥 链接:http://www.jianshu.com/p/9ab1205f5166 有时候我们需要处理图片,比如改变大小,旋转,截取等等,所以今天说一说图片处理相关的一些操作. 本 ...
- iOS开发 - 一个天真的搜索控制器的独白
文/Azen(简书作者)原文链接:http://www.jianshu.com/p/6d5327111511著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 正文 一.关于横向模块开发 ...
- [Adruino]XBEE 无线数据传输实际操作
双轮小车制作实例代码 引用:http://hi.baidu.com/dlfla84/item/52b89017a6209c5cf1090e9b 双轮小车制作 2009-6-12 初步完成了串口数据缓存 ...
- 安卓弹出对话框——Alertdialog
在Android开发当中,在界面上弹出一个Dialog对话框使我们经常需要做的,本篇随笔将详细的讲解Dialog对话框这个概念,包括定义不同样式的对话框. 一.Dialog 我们首先来看看androi ...
- iOS 真机测试的一些报错
1.连了手机热点 fix Issue后出现提示框:No Devices Registered:Creating a provisioning profile requires one or more ...
- IOS开发UI篇之自动滚动图片
我们在做项目是有时候会遇到设置自动滚动图片,所以我自己也遇到过两次.觉得他是一个挺有意思东西,所以做了下总结 DEMO: .h #import <UIKit/UIKit.h> @inter ...
- 0. SQL Server监控清单
数据库服务器的监控可大致分为两类: (1) 状态监控:数据库服务器有没有在健康地运行? (2) 性能监控:健康运行的同时,有没有性能问题?可不可以更快些? 一. 服务器 1. 状态监控 (1) 服务器 ...
- Java多线程之锁
首先是synchronized 关键字 他可以用于声明方法,也可以用于申明代码块.我们看看三个例子: public class SynchronizedDemo1 { public synchroni ...
- sysobjects.xtype介绍
SQL Server数据库的一切信息都保存在它的系统表格里. 在大多数情况下,对你最有用的两个列是Sysobjects.name和Sysobjects.xtype.前面一个用来列出待考察对象的名字,而 ...
- Ajax 传统的异步登陆
这是一个传统的异步登陆,利用Ajax实现的,主要代码如下: 客户端代码: var http; function Button1_onclick() { if (window.ActiveXObject ...