http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 31099|回复: 22

[源码分享] C#Winform INI文件读与写

[复制链接]
发表于 2013-4-3 12:18:36 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Text;

  5. namespace IniFile
  6. {

  7.     /// <summary>
  8.     /// ini文件读与写
  9.     /// </summary>
  10.     public class ClsIniFile
  11.     {
  12.         //文件INI名称
  13.         public string Path;
  14.         ////声明读写INI文件的API函数
  15.         [DllImport("kernel32")]
  16.         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  17.         [DllImport("kernel32")]
  18.         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
  19.         //类的构造函数,传递INI文件名
  20.         public ClsIniFile(string inipath){ Path = inipath; }
  21.         /// <summary>
  22.         /// 写入INI文件
  23.         /// </summary>
  24.         /// <param name="Section">配置节</param>
  25.         /// <param name="Key">键名</param>
  26.         /// <param name="Value">键值</param>
  27.         public void IniWriteValue(string Section, string Key, string Value)
  28.         {
  29.             WritePrivateProfileString(Section, Key, Value, this.Path);
  30.         }

  31.         /// <summary>
  32.         /// 读取制定INI文件键值
  33.         /// </summary>
  34.         /// <param name="Section">配置节</param>
  35.         /// <param name="Key">键名</param>
  36.         /// <returns></returns>
  37.         public string IniReadValue(string Section, string Key)
  38.         {
  39.             StringBuilder temp = new StringBuilder(255);
  40.             int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.Path);
  41.             return temp.ToString();
  42.         }
  43.     }
  44. }
复制代码
INI文件简单写于读类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using IniFile;//ini简单类

  11. namespace Winform_读写INI文件
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }

  19.         private void but_Click(object sender, EventArgs e)
  20.         {
  21.             string struser = this.user.Text.Trim(), strpwd = this.pwd.Text.Trim();
  22.             #region 写入INI
  23.             string sPath = Application.StartupPath + "\\FondoServer.ini";//写入到软件当前目录
  24.              ClsIniFile cl = new ClsIniFile(sPath);
  25.             cl.IniWriteValue("FondoServer", "user", struser);
  26.             cl.IniWriteValue("FondoServer", "pwd", strpwd);
  27.             /*多个如下添加就是咯*/
  28.            // cl.IniWriteValue("FondoServer", "user", struser);
  29.            // cl.IniWriteValue("FondoServer", "pwd", strpwd);  
  30.             MessageBox.Show("INI文件写入成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  31.             #endregion
  32.         }

  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             ReadDataini();
  36.            
  37.         }
  38.         private void ReadDataini()
  39.         {
  40.             string sPath = Application.StartupPath + "\\FondoServer.ini";//读取软件当前目录
  41.             ClsIniFile ini = new ClsIniFile(sPath);
  42.             this.user.Text = ini.IniReadValue("FondoServer", "user");
  43.             this.pwd.Text = ini.IniReadValue("FondoServer", "pwd");
  44.             /*多个如下添加就是咯*/
  45.             //this.user.Text = ini.IniReadValue("FondoServer", "user");
  46.             //this.pwd.Text = ini.IniReadValue("FondoServer", "pwd");
  47.          
  48.         }

  49.         private void linkLabel1_Click(object sender, EventArgs e)
  50.         {
  51.       
  52.             System.Diagnostics.Process.Start("http://www.sufeinet.com");
  53.         }

  54.       
  55.     }
  56. }
复制代码

效果图

效果图

源码


Winform 读写INI文件.tar.gz

55.64 KB, 下载次数: 1151, 下载积分: 金钱 -1

源码

本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2013-4-3 12:20:26 | 显示全部楼层
占个沙发,第一次发源码。
发表于 2013-4-3 12:47:05 | 显示全部楼层
强烈支持楼主ing……
 楼主| 发表于 2013-4-6 16:18:38 | 显示全部楼层
苏老大也给我来个勋章呗!!!!随你给个勋章。灌水的也可以噻。
发表于 2013-4-6 16:19:46 | 显示全部楼层
梦如人生 发表于 2013-4-6 16:18
苏老大也给我来个勋章呗!!!!随你给个勋章。灌水的也可以噻。

已送
发表于 2013-5-7 20:52:45 | 显示全部楼层
勋章有什么用阿,大虾们!
发表于 2013-5-7 21:08:55 | 显示全部楼层
lhpert 发表于 2013-5-7 20:52
勋章有什么用阿,大虾们!

好看,是尊贵的象征
发表于 2013-6-13 23:33:18 | 显示全部楼层
这个写辅助登录刚好用要用到。。果断了。。。
发表于 2013-10-18 19:27:24 | 显示全部楼层
非常感谢您!
发表于 2013-11-1 17:29:18 | 显示全部楼层
好东西,拿来学习了,谢谢分享
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-26 00:36

© 2014-2021

快速回复 返回顶部 返回列表