Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 试图打开一个.ini文件,但在C语言中不断出现错误#_C#_File_Ini - Fatal编程技术网

C# 试图打开一个.ini文件,但在C语言中不断出现错误#

C# 试图打开一个.ini文件,但在C语言中不断出现错误#,c#,file,ini,C#,File,Ini,我对编码还不熟悉,所以要友善。我试图在一个程序中打开一个.ini文件,但这样做时,我总是会遇到这个错误 mscorlib.dll中发生类型为“System.FormatException”的未处理异常 其他信息:输入字符串的格式不正确 我的代码: private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog o1 = new OpenFileDialog(); o1.Filter = "I

我对编码还不熟悉,所以要友善。我试图在一个程序中打开一个.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;
            }
       }
   }

}

然后
inti=int.parse(reini)以绿色标记

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

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
键。以上代码已更新。有一个添加到启动中的
,我想你需要这个。

正如斯特里比雪夫所说,在这些情况下,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";
       }
   }

你能显示你的INI文件吗,特别是用户ID键的值。这是INI文件的什么实现?这不是一个标准的.NET类。这是我要在4个文本框中展开的.ini文件的内容,很明显,用户ID是字符串,而不是int。也许,您不需要对integer进行任何解析?为什么不使用字符串变量呢?谢谢Stribizev,现在我需要修复复选框和组合框。我有一个组合框和5个复选框。@AdmiralKitteh:Updated。我讨厌人们否决像这样有用的东西。考虑到这个答案作为公认的解决方案,人们应该投票赞成,而不是投票否决。答案是1。