7

出于某种原因,在我的 SaveFileDialog 之后,我的应用程序将永远不会显示 MessageBox。有什么我想念的吗?或者这是一个线程问题?

我使用 VS 2010 Express 将应用程序作为 Windows 窗体应用程序运行。

我没有任何例外。

补充:当我单步执行代码时,一切似乎都很顺利。这很奇怪,所以我认为这是一个时间问题。

LarsTech 和其他人指出,MessageBoxes 确实出现了,但是焦点消失了;换句话说,MessageBox 被推到其他窗口后面或最小化。这是个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
namespace SpeedDating
{
    class Program
    {
         [STAThread]
        static void Main(string[] args)
        {

        string filename = "test.test"; // args[0];
        string ext = filename.Substring(filename.LastIndexOf('.'));
        SaveFileDialog dialog = new SaveFileDialog();
        dialog.Title = "SpeedDating App";
        dialog.RestoreDirectory = true;
        dialog.CheckFileExists = false;
        dialog.CheckPathExists = false;
        dialog.FileName = DateTime.Now.ToString("yyyyMMdd") + ext;

        DialogResult result = dialog.ShowDialog();
        if (result == DialogResult.OK && dialog.FileName != "")
        {
            try
            {
                FileStream outfs = File.Create(dialog.FileName);
                FileStream infs = File.Open(filename, FileMode.Open);
                infs.CopyTo(outfs);
                infs.Close();
                outfs.Close();
            }
            catch (NotSupportedException ex)
            {
                MessageBox.Show("Probably removed the original file.");
            }
        }
        else
        {
            MessageBox.Show("No path found to write to.");
        }

        MessageBox.Show("I came here and all I got was this louzy printline");

        }
    }
}
4

4 回答 4

4

我试过这个,它立即显示:

 MessageBox.Show(new Form() { WindowState = FormWindowState.Maximized, TopMost = true }, "You clicked Cancel button", "Cancel");
于 2013-05-31T14:17:00.080 回答
2

为您的消息框试试这个。

MessageBox.Show(this,"Probably removed the original file.");
于 2013-05-31T13:58:54.543 回答
1

我创建了一个新项目并粘贴了您的代码,它对我有用。确保在运行之前完成了完全重建。此外,使用这一行:

dialog.FileName = DateTime.Now.ToString(format) + "." + ext;

该对话框将以文件名开头。因此,只有点击取消按钮(假设您没有先清除保存对话框)才会触发消息框。无论哪种方式,无论哪种方式,如果您的 IF 测试失败,我都会弹出消息框。你的代码看起来不错。

于 2013-05-31T13:35:58.947 回答
1

也许您应该SaveFileDialog在 MessageBox 调用之前将其放入 using 以确保其处置:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
namespace SpeedDating
{
    class Program
    {
         [STAThread]
        static void Main(string[] args)
        {
            string filename = "test.test"; // args[0];
            string ext = filename.Substring(filename.LastIndexOf('.'));
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Title = "SpeedDating App by K.Toet";
                dialog.RestoreDirectory = true;
                dialog.CheckFileExists = false;
                dialog.CheckPathExists = false;
                dialog.FileName = DateTime.Now.ToString("yyyyMMdd") + ext;

                DialogResult result = dialog.ShowDialog();
                if (result == DialogResult.OK && dialog.FileName != "")
                {
                    try
                    {
                        FileStream outfs = File.Create(dialog.FileName);
                        FileStream infs = File.Open(filename, FileMode.Open);
                        infs.CopyTo(outfs);
                        infs.Close();
                        outfs.Close();
                    }
                    catch (NotSupportedException ex)
                    {
                        MessageBox.Show("Probably removed the original file.");
                    }
                }
                else
                {
                    MessageBox.Show("No path found to write to.");
                }
            }

            MessageBox.Show("I came here and all I got was this louzy printline");
        }
    }
}
于 2013-05-31T13:47:49.153 回答