C#读写INI文件示例demo

  C#读写INI文件示例demo

 

  注意事项:如果没有读取ini文件和写入的权限,读写ini文件时不会报错的。所以如果发现读取或者写入不了ini文件,自己检查一下ini文件是否有运行网站用户的写入权限

 

    C#读写INI文件示例demo源代码

INIFileDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xxx.aspx.cs" Inherits="INIFileDemo" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<title>C#读写INI文件示例demo</title>
<style type="text/css">
body {
background-color: #c4d8ed;
}
.STYLE6 { color: #000000;
font-weight: bold;
}
.STYLE7 {color: #135294}
.STYLE8 {
color: #FF0000;
font-weight: bold;
}</style>
<body>
    <form id="form1" runat="server">
      <div> 
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#284885">
                <tr>
                  <td width="20%" colspan="2" align="left" valign="middle" bgcolor="#E4EDF9"><span style="width: 186px"> </span><span class="td2"><span class="STYLE6"> </span></span> <br />
                    <table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="C4D8ED">
                      <tr>
                        <td height="28" bgcolor="#F1F3F5"><span class="STYLE7">&nbsp;[设置]&nbsp;</span></td>
                      </tr>
                    </table>
                  <br /><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td><table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="C4D8ED">
            <tr>
              <td colspan="2" align="left" bgcolor="F1F3F5" style="height: 26px">&nbsp;</td>
            </tr>
            <tr>
              <td width="32%" height="30" align="right" bgcolor="F1F3F5" style="height: 26px">
                 A帐号:</td>
                <td width="68%" height="26" align="left" bgcolor="F1F3F5" style="height: 26px">
                  &nbsp; 
                  <asp:TextBox CssClass="td1" ID="userid"  runat="server" Width="243px"></asp:TextBox>
              &nbsp;</td>
            </tr>
           <tr>
              <td width="32%" height="30" align="right" bgcolor="F1F3F5" style="height: 26px">
                 B比例:</td>
                <td width="68%" height="26" align="left" bgcolor="F1F3F5" style="height: 26px">
                  &nbsp; 
                  <asp:TextBox CssClass="td1" ID="userid1"  runat="server" Width="243px"></asp:TextBox>
              &nbsp;</td>
            </tr>
           
            <tr>
              <td height="30" align="right" bgcolor="F1F3F5">
                  确认修改:</td>
                <td height="26" align="left" bgcolor="F1F3F5">
                  &nbsp;
                  <asp:Button CssClass="bt2" ID="Button1" runat="server" Text="提交" Width="134px" OnClick="Button1_Click" />
              &nbsp;&nbsp;</td>
            </tr>
            <tr>
              <td height="83" colspan="2" align="center" bgcolor="F1F3F5"><table width="98%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="32%" height="69">&nbsp;</td>
                  <td width="68%" align="left"><span class="STYLE8">注意:<br>
   内容            </span></td>
                </tr>
              </table>
                <br /></td>
              </tr>
            </table></td>
        </tr>
        </table></td>
                </tr>
                <tr>
                  <td valign="top" bgcolor="#F1F3F5">&nbsp;</td>
                  <td valign="top" bgcolor="#F1F3F5">&nbsp;</td>
                </tr>
            </table></td>
          </tr>
        </table>
        <br />
        <br />
      
    </div></form>
</body>
</html>

INIFileDemo.aspx.cs

using System;
using System.Text;
using System.Runtime.InteropServices;
public partial class INIFileDemo : System.Web.UI.Page
{
    /// <summary>
    /// INIFILE操作类
    /// </summary>
    public class INIFILE
    {
        /// <summary>
        /// INI文件路径
        /// </summary>
        public string FilePath;
        //////////////////////////////////////////////////////////////////
        //注意下面2个方法,如果没有读取ini文件和写入的权限,读写ini文件时不会报错
        //所以如果发现读取或者写入不了ini文件,自己检查一下ini文件是否有运行网站用户的写入权限
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        //////////////////////////////////////////////////////////////////
        /// <summary>
        /// 构造函数
        /// </summary>
        public INIFILE() { }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="INIPath">文件路径</param>
        public INIFILE(string INIPath) { FilePath = INIPath; }
        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="Section">项目名称(如 [TypeName] )</param>
        /// <param name="Key">键</param>
        /// <param name="Value">值</param>
        public void WriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.FilePath); }
        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="Section">项目名称(如 [TypeName] )</param>
        /// <param name="Key">键</param>
        public string ReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.FilePath);
            return temp.ToString();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            INIFILE ini = new INIFILE(Server.MapPath("~/config.ini"));
            Response.Write(ini.FilePath);
            string strReturnValue = ini.ReadValue("public", "USERID");
            this.userid.Text = strReturnValue;
            string strReturnValue1 = ini.ReadValue("public", "CHUPER");
            this.userid1.Text = strReturnValue1;
        }     
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        INIFILE ini = new INIFILE(Server.MapPath("~/config.ini"));
        string wstr = this.userid.Text.ToString().Trim();
        ini.WriteValue("public", "USERID", wstr);
        string wstr1 = this.userid1.Text.ToString().Trim();
        ini.WriteValue("public", "CHUPER", wstr1);
        Response.Write("操作成功!"+DateTime.Now+"|"+ini.FilePath);
    }
}

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:C#读写INI文件示例demo

评论(0)Web开发网
阅读(105)喜欢(0)Asp.Net/C#/WCF