尝试打开 .ini 文件,但我在 C# 中不断收到错误消息

Trying to open a .ini file but i keep getting an error in C#

我是编码新手,请多多关照。我试图在程序中打开一个 .ini 文件,但我这样做时总是收到此错误。

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

我的代码:

private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog o1 = new OpenFileDialog();
        o1.Filter = "INI File |*.ini";
        if (o1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            INIFile ini = new INIFile(o1.FileName);
            string reini = ini.Read ("Profile Settings", "User ID");
            int i = int.Parse(reini);
            textBox1.Text = i.ToString();
            textBox3.Text = i.ToString();
            textBox4.Text = i.ToString();
            textBox5.Text = i.ToString();
            string rechini = ini.Read("Startup", "Check");
            if(rechini == "checked")
            {
                checkBox1.Checked = true;
            }
            else
            {
                checkBox1.Checked = false;
            }
       }
   }

}

然后 int i = int.parse(reini); 被标记为绿色

用户 ID 很可能是字母数字字符串。如果用户 ID 可以是字母数字和整数类型,那么使用 .TryParse() 会更安全。

int i = -1;
string user_id = string.Empty;
if (!int.TryParse(reini, out i))
{
    user_id = reini;
}
if (!String.IsNullOrEmpty(user_id)) // it is an alphanumeric
{
}
else // it is an integer, use i
{
}

更新 由于您的用户 ID 是一个字符串,因此只需使用一个字符串:

private void btnOpen_Click(object sender, EventArgs e)
{
        OpenFileDialog o1 = new OpenFileDialog();
        o1.Filter = "INI File |*.ini";
        if (o1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            INIFile ini = new INIFile(o1.FileName);
            string user_id = ini.Read ("Profile Settings", "User ID");
            textBox1.Text = user_id;
            textBox3.Text = user_id;
            textBox4.Text = user_id;
            textBox5.Text = user_id;
            string rechini = ini.Read("Startup", "Add To Startup");
            if(rechini == "checked")
            {
                checkBox1.Checked = true;
            }
            else
            {
                checkBox1.Checked = false;
            }
       }
   }

}

更新2

您的 Startup INI 文件部分中没有 Check 密钥。上面的代码已更新。有Add To Startup,估计你需要这个

正如 stribizhev 所说,在这些情况下,TryParse 优于 Parse。

尤其如此,因为您的用户 ID 是一串字符,而不是数字。

此外,'Startup' 'checked' 将始终失败,因为该设置名为 'Add To Startup'(除非您有另一个名为 'checked' 的设置,该设置不在您的文件中提供)。

所以,改为:

    private void btnOpen_Click(object sender, EventArgs e)
    {
        OpenFileDialog o1 = new OpenFileDialog();
        o1.Filter = "INI File |*.ini";
        if (o1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            INIFile ini = new INIFile(o1.FileName);
            string reini = ini.Read("Profile Settings", "User ID");
            textBox1.Text = reini;
            textBox3.Text = reini;
            textBox4.Text = reini;
            textBox5.Text = reini;
            string rechini = ini.Read("Startup", "Add To Startup");
            checkBox1.Checked = rechini == "checked";
       }
   }