试图打开一个.ini文件,但我一直在C#中出错

时间:2015-03-07 21:38:52

标签: c# file ini

我是编码的新手,所以要好好。我正在尝试在程序中打开.ini文件,但是当我这样做时,我一直收到这个错误。

  

mscorlib.dll中发生未处理的“System.FormatException”类型异常

     

其他信息:输入字符串的格式不正确。

我的代码:

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);标记为绿色

2 个答案:

答案 0 :(得分:1)

用户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;
            }
       }
   }

}

<强> UPDATE2

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

答案 1 :(得分:1)

正如stribizhev所说,在这些情况下,TryParse比Parse更好。

尤其如此,因为您的用户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 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";
       }
   }
相关问题