C#的ini文件读取

问题遇到的现象和发生背景

利用.Net的C#做了一个登录的页面,里面包含了注册。注册使用点击按钮跳转到注册页面,在账号密码上面输入的信息读取到ini文件中

问题相关代码,请勿粘贴截图

给大家看下这个是登录页面的代码:
using System;
using System.Collections;
using System.Windows.Forms;
using System.Configuration;
using Login;

namespace Demo
{
public partial class two : Form
{
IniFiles ini = new IniFiles(Application.StartupPath + @"\MyConfig.INI");

    public two()
    {
        InitializeComponent();
    }

    //载入事件
    private void two_Load(object sender, EventArgs e)
    {
        user_name.Text = IniFiles.ReadString("Login", "username");

        if (ConfigurationManager.AppSettings["rememberme"].Equals("true"))
        {
            password.Text = ConfigurationManager.AppSettings["password"];
            remember.Checked = true;
        }
        
    }

    private void sign_in_Click(object sender, EventArgs e)
    {
        this.Hide();
        one no = new one();
        no.ShowDialog();

        Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        if (automatic.Checked)
        {
            cfa.AppSettings.Settings["autologin"].Value = "trye";
            cfa.AppSettings.Settings["rememberme"].Value = "true";
            cfa.AppSettings.Settings["name"].Value = user_name.Text;
            cfa.AppSettings.Settings["password"].Value = password.Text;
            cfa.Save();
        }
        else
        {
            if (remember.Checked)
            {
                cfa.AppSettings.Settings["autologin"].Value = "false";
                cfa.AppSettings.Settings["rememberme"].Value = "false";
                cfa.AppSettings.Settings["name"].Value = "";
                cfa.AppSettings.Settings["password"].Value = "";
            }
        }
       // cfa.Save();
    }

    private void close_Click(object sender, EventArgs e)
    {
        Close();
    }

    //本文框是否有文本
    private Boolean textboxHasText = false;

    private void user_name_Leave(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(user_name.Text))
        {
            lb_name.Text = "用户名是必填项";
        }
        else
        {
            lb_name.Text = "";
        }
    }

    private void user_name_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void automatic_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void register_Click(object sender, EventArgs e)
    {
        this.Hide();
        three th = new three();
        th.ShowDialog();
    }
}

}

然后我又创建了一个IniFiles的类里面的代码也给大家看一下

using System;
using System.Text;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.InteropServices;

namespace Login
{
public class IniFiles
{
public static string FileName; //INI文件名
//string path = System.IO.Path.Combine(Application.StartupPath, "新建文本文档.ini");

    #region 声明读写INI文件的API函数
    [DllImport("kernel32")]
    private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);

    #endregion


    #region 类的构造函数,传递INI文件名
    public IniFiles(string AFileName)
    {
        // 判断文件是否存在
        FileInfo fileInfo = new FileInfo(AFileName);

        //Todo:搞清枚举的用法
        if ((!fileInfo.Exists))
        { //|| (FileAttributes.Directory in fileInfo.Attributes))
            //文件不存在,建立文件
            System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
            try
            {
                sw.Write("#表格配置档案");
                sw.Close();
            }
            catch
            {
                throw (new ApplicationException("Ini文件不存在"));
            }
        }
        //必须是完全路径,不能是相对路径
        FileName = fileInfo.FullName;
    }
    #endregion


    #region 写INI文件
    public static void WriteString(string Section, string Key, string Value)
    {
        if (!WritePrivateProfileString(Section, Key, Value, FileName))
        {
            throw (new ApplicationException("写Ini文件出错"));
        }
    }
    #endregion


    #region 读取INI文件指定
    public static string ReadString(string Section, string Key)
    {
        Byte[] Buffer = new Byte[65535];
        int bufLen = GetPrivateProfileString(Section, Key, "", Buffer, Buffer.GetUpperBound(0), FileName);
        //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
        string s = Encoding.GetEncoding(0).GetString(Buffer);
        s = s.Substring(0, bufLen);
        return s.Trim();
    }
    #endregion
}

}
并且在C:\Users\PXNB\source\repos\Demo\Demo\bin\Debug下面创建了一个MyConfig.ini的一个记事本
对了还有一个注册的页面代码也给大家
using Login;
using System;
using System.Windows.Forms;

namespace Demo
{
public partial class three : Form
{
two tw = new two();

    public three()
    {
        InitializeComponent();
    }
    private void three_Load(object sender, EventArgs e)
    {

    }

    private void confirm_Click(object sender, EventArgs e)
    {
        this.Hide();
        two tw = new two();
        tw.ShowDialog();
    }

    private void username_Leave(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(username.Text))
        {
            tet_username.Text = "用户名不能是空";
        }
        else
        {
            tet_username.Text = "";
        }
    }

    private void password_Leave(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(password.Text))
        {
            tet_password.Text = "密码不能是空";
        }
        else
        {
            tet_password.Text = "";
        }
    }

    private void confirm_password_Leave(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(confirm_password.Text))
        {
            tet_confirm_password.Text = "两次密码输入不一致";
        }
        else
        {
            tet_confirm_password.Text = "";
        }
    }
}

}

运行结果及报错内容
我的解答思路和尝试过的方法

完全是从java转到C#的不是很懂C#虽然很多地方很像但是还是吃不透

我想要达到的结果

能思路清晰一下,最好能附带上自己的代码麻烦了

有什么问题吗

检查你的 IniFiles,是否抄错了

没报错而是空的话检查一下传到GetPrivateProfileString的参数是不是都正确呢