双击相关文件时,文件将无法打开和程序崩溃

时间:2015-01-30 22:42:19

标签: c# winforms double-click file-association

我在关联程序中打开文件时遇到了一个特殊问题。首先,我双击一个文件,单击“打开方式...”,然后单击我的程序到程序的项目文件中的Debug文件夹并运行可执行文件。这是为了模拟在与之关联的程序中打开文件,就像程序实际安装在我的计算机上一样。

以下是Program.cs中的完整代码:

namespace TriviaAuthor_v10
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmSplashScreen());
            if (args.Length > 0)
                Application.Run(new frmMain(args[0]));
            else
                Application.Run(new frmMain());
        }
    }
}

现在这里是主窗体的两个构造函数的代码:

    public frmMain(string autoopenfilepath)
    {
        InitializeComponent();
        filepath = autoopenfilepath;
        OpenTheFile(filepath);
    }

    public frmMain()
    {
        InitializeComponent();
    }

这是打开文件的代码:

    private void OpenTheFile(string ThisFilePath)
    {
        // First we get the filename.
        filename = Path.GetFileName(ThisFilePath);
        FilenameSansExtension = Path.GetFileNameWithoutExtension(ThisFilePath);
        // Create a file stream.
        FileStream fs = new FileStream(ThisFilePath, FileMode.Open, FileAccess.Read);
        // Create the writer for data.
        BinaryReader br = new BinaryReader(fs);
        GameInfo.GameTitle = br.ReadString();
        GameInfo.GameAuthor = br.ReadString();
        GameInfo.DateCreated = br.ReadString();
        GameInfo.NumberOfQuestions = br.ReadInt32();
        GameInfo.TitlePageImagePresent = br.ReadBoolean();
        GameInfo.TitlePageImage = br.ReadString();
        GameInfo.IntroScreenAudioPresent = br.ReadBoolean();
        GameInfo.IntroScreenAudio = br.ReadString();
        GameInfo.FinalScoreAudioPresent = br.ReadBoolean();
        GameInfo.FinalScoreAudio = br.ReadString();
        GameInfo.ActiveQuestion = br.ReadInt32();

        if (GameInfo.NumberOfQuestions > 0)
        {
            for (int i = 0; i < GameInfo.NumberOfQuestions; i++)
            {
                clsQuestionClass Question = new clsQuestionClass();

                Question.NewQuestion = br.ReadString();
                Question.Points = br.ReadInt32();
                Question.QuestionType = br.ReadInt32();
                Question.QuestionImagePresent = br.ReadBoolean();
                Question.QuestionImage = br.ReadString();

                Question.QuestionAudioPresent = br.ReadBoolean();
                Question.QuestionAudio = br.ReadString();

                Question.IncludeTimer = br.ReadBoolean();
                Question.TimerTime = br.ReadInt32();
                Question.TickTock = br.ReadBoolean();

                Question.AIsChecked = br.ReadBoolean();
                Question.AnswerA = br.ReadString();
                Question.AIsCorrect = br.ReadBoolean();

                Question.BIsChecked = br.ReadBoolean();
                Question.AnswerB = br.ReadString();
                Question.BIsCorrect = br.ReadBoolean();

                Question.CIsChecked = br.ReadBoolean();
                Question.AnswerC = br.ReadString();
                Question.CIsCorrect = br.ReadBoolean();

                Question.DIsChecked = br.ReadBoolean();
                Question.AnswerD = br.ReadString();
                Question.DIsCorrect = br.ReadBoolean();

                Question.TrueOrFalse = br.ReadBoolean();

                Question.FillInBlankAnswer = br.ReadString();

                Question.AnswerResponseImagePresent = br.ReadBoolean();
                Question.AnswerResponseImage = br.ReadString(); ;
                Question.CorrectAnswerResponse = br.ReadString();
                Question.IncorrectAnswerResponse = br.ReadString();

                Question.CorrectAnswerResponseAudioPresent = br.ReadBoolean();
                Question.CorrectAnswerResponseAudio = br.ReadString();
                Question.IncorrectAnswerResponseAudioPresent = br.ReadBoolean();
                Question.IncorrectAnswerResponseAudio = br.ReadString();

                Questions.Add(Question);
                Questions.Count();
            }
        }
        fs.Close();
        br.Close();
        QuestionIndex = GameInfo.ActiveQuestion;
        LoadGameIntoGameGUI(Questions[QuestionIndex]);
        this.Text = "Trivia Author v1.0 - " + FilenameSansExtension;
        ProjectNeedsSaving = false;
        saveAsToolStripMenuItem.Enabled = closeprojecttoolStripMenuItem1.Enabled = exportgametoolStripMenuItem.Enabled =
        printToolStripMenuItem.Enabled = printPreviewToolStripMenuItem.Enabled = tsbtnProjectClose.Visible =
        ProjectIsOpen = saveToolStripMenuItem.Enabled = tsbtnSaveProject.Enabled = btnShowProjectReview.Enabled = true;

        UpdateGameSummary();
    }

注意:“OpenTheFile(string ThisFilePath)”用于使用OpenFileDialog打开文件,当我尝试通过双击打开文件时。

所以这就是问题:当我在Visual Studio 2013中运行程序然后打开文件(使用OpenFileDialog)时,文件打开没有任何问题。但是当我尝试通过双击打开文件并使用程序的Debug文件夹中的可执行文件打开它时,我看到程序的启动画面然后程序中止。它

在我看来,文件的路径正确地被转发到“OpenTheFile()”。并且因为程序在Visual Studio外部运行,所以我没有收到任何错误消息,甚至没有来自操作系统。

1 个答案:

答案 0 :(得分:0)

让我们重新开始吧。您可以在应用程序的最开始检查参数。如果它达到LoadGameIntoGameGUI()并在那里(在读取文件后)粉碎,则意味着某些东西被传递给程序。当你提到你已经添加了一个消息框时,我假设现在你运行了正确的程序版本,但你仍然有一个例外。它是完全相同的例外吗?如果是这样,您是否介意发布LoadGameIntoGameGUI的代码?

顺便说一下,if这里是不必要的,如果GameInfo.NumberOfQuestions==0循环将被忽略:

 if (GameInfo.NumberOfQuestions > 0)
    {
        for (int i = 0; i < GameInfo.NumberOfQuestions; i++)

这些数字和问题是什么?你将其中一个传递给LoadGameIntoGameGUI,你确定它是正确的吗?您可以添加日志文件并检查输入。像File.WriteAllText("your log.txt", question.ToString());这样的东西(确保ToString返回有意义的东西)。