欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net操作ini文件示例

程序员文章站 2024-02-24 21:28:58
复制代码 代码如下:using system;using system.data;using system.configuration;using system.web;u...

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

using system.runtime.interopservices;
using system.text;

namespace createwebdir
{
    /// <summary>
    /// inifile 的摘要说明
    /// </summary>
    public class inifile
    {

        public string path;

        public inifile(string inipath)
        {
            path = inipath;
        }

        [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);

        public void iniwritevalue(string section, string key, string value)
        {
            writeprivateprofilestring(section, key, value, this.path);
        }


        public string inireadvalue(string section, string key)
        {
            stringbuilder temp = new stringbuilder(255);

            int i = getprivateprofilestring(section, key, "", temp, 255, this.path);

            return temp.tostring();

        }
    }
}

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

using system.io;

public partial class user_default : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        createwebdir.inifile ab = new createwebdir.inifile(@"f:\test");

        string inifile = @"f:\test\test.ini";
        if (!file.exists(inifile))
        {
            using (filestream fs = file.create(inifile))
            {
                fs.close();
            }
        }

        string[] args = new string[10];

        createwebdir.inifile myini = new createwebdir.inifile(inifile);

        for (int i = 0; i < args.length; i++)
        {
            args[i] = convert.tostring(i + i * i * i);
            myini.iniwritevalue("webdir", "arg" + i.tostring(), args[i]);
        }
    }
}

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

using system.io;

public partial class user_default : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        createwebdir.inifile ab = new createwebdir.inifile(@"f:\test\test.ini");

        response.write(ab.inireadvalue("webdir", "arg5"));
    }
}