0

当我使用 Try/Catch 打开文件时,当我尝试打开不存在的文件时,程序会显示一条内置消息,而不是我在 Catch 部分中的消息。出了什么问题,我错过了什么?

 public void ReadFromFile(MainFrame obj, string filePath)
    {
        try
        {
            filestream = new FileStream(filePath, FileMode.Open);
            BinaryFormatter b = new BinaryFormatter();
            var animals2 = (List<Animal>)b.Deserialize(filestream);

            foreach (Animal animal in animals2)
            {
                AddAnimalToList(animal);
                obj.UppdateListOfRegistredAnimals(animal.ID, animal.Name, animal.Age, animal.Gender);

            }
            obj.UpdateId(animals.Count());
        }
        catch
        {
            MessageBox.Show("Test", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        finally
        {
            filestream.Close();
        }
    }

编辑:我发现内置消息的原因是在上面的代码之前的某个地方!下面处理来自 openFileDialog 的事件的代码一定有问题,因为尽管有多个消息框,但未显示!我做错了什么!?帮助是preciated!

private void menuFileOpen_Click(object sender, EventArgs e)
    {
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        string file = openFileDialog1.FileName;

        if (result == DialogResult.OK)
        {
            MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            try
            {
                MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                motelManager.ReadFromFile(this, file); // Smart lösning!!

            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Error message", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }

找不到文件消息

4

2 回答 2

1

这可能是因为您尝试在不检查它是否为非空的情况下关闭filestream您的块。finally如果

filestream = new FileStream(filePath, FileMode.Open);

失败,

finally
{
    filestream.Close();
}

在 filestream 为空的情况下执行。

尝试将其更改为

finally
{
    if (filestream != null) filestream.Close();
}

或者更好的是,将你的包裹filestream在一个using块中。

using (filestream = new FileStream(filePath, FileMode.Open))
{
    // Do stuff with your filestream
} // filestream.Dispose() automatically called, which in turn calls .Close()
于 2012-07-27T06:13:42.553 回答
1

试试这个

if(!File.Exists(filePath))
   MessageBox.Show("Not found", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

更多信息 http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

private void menuFileOpen_Click(object sender, EventArgs e)
    {
        openFileDialog1.CheckFileExists = false;
        openFileDialog1.CheckPathExists = false;

        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.


        if (result == DialogResult.OK)
        {
            string file = openFileDialog1.FileName;


            MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            try
            {
                MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                motelManager.ReadFromFile(this, file); // Smart lösning!!

            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Error message", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }

如果您不想要文件未找到的默认消息,请使用

        openFileDialog1.CheckFileExists = false;
        openFileDialog1.CheckPathExists = false;
于 2012-07-27T06:14:28.227 回答