简体   繁体   English

双击关联的文件时,文件无法打开并且程序崩溃

[英]When Double-Clicking On Associated File, File Won't Open And Program Crashes

I'm having a peculiar problem with opening a file in its associated program. 我在其关联程序中打开文件时遇到一个特殊问题。 First I double-click on a file, click "Open with...", then I click my way to the Debug folder in my program's project file and run the executable. 首先,我双击文件,然后单击“打开方式...”,然后单击程序项目文件中的Debug文件夹并运行可执行文件。 This is to simulate opening a file in the program associated with it as if the program were actually installed on my computer. 这是为了模拟在与之关联的程序中打开文件,就像该程序实际上已安装在我的计算机上一样。

Here's the entire code from Program.cs: 这是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());
        }
    }
}

Now here's the code for the two constructors for the main form: 现在,这里是主要形式的两个构造函数的代码:

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

    public frmMain()
    {
        InitializeComponent();
    }

And here's the code for opening the file: 这是打开文件的代码:

    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();
    }

Note: "OpenTheFile(string ThisFilePath)" is used for both opening the file using an OpenFileDialog and when I try to open a file by double-clicking on it. 注意:“ OpenTheFile(string ThisFilePath)”既用于使用OpenFileDialog打开文件,也用于当我尝试通过双击打开文件时。

So here's the problem: when I run the program in Visual Studio 2013 and then open the file (using OpenFileDialog) the file opens with no problems. 所以这是问题所在:当我在Visual Studio 2013中运行程序,然后打开文件(使用OpenFileDialog)时,文件打开没有问题。 But when I try to open the file by double-clicking it and opening it with the executable in the program's Debug folder, I see the program's splash screen and then the program aborts. 但是,当我尝试通过双击文件并在程序的Debug文件夹中使用可执行文件打开文件时,我看到了程序的启动屏幕,然后程序中止。 It

looks to me like the file's path is being relayed to "OpenTheFile()" correctly. 在我看来,文件路径已正确中继到“ OpenTheFile()”。 And because the program is running outside Visual Studio, I get no error messages, not even from the operating system. 并且由于该程序在Visual Studio外部运行,所以我什至没有从操作系统中都收到错误消息。

Let's start again. 让我们重新开始。 You check arguments at the very beginning of your application. 您可以在应用程序的最开始检查参数。 If it reaches LoadGameIntoGameGUI() and crushes there (after reading a file), it means that something is passed to the program. 如果到达LoadGameIntoGameGUI()并在LoadGameIntoGameGUI()在读取文件后),则表示已将某些内容传递给程序。 As you mention that you've added a message box, I assume that now you run the right version of the program, but you still have an exception. 正如您提到的那样,您已经添加了一个消息框,我假设现在您运行的是正确版本的程序,但是仍然有例外。 Is it exactly the same exception? 完全一样的例外吗? If so, would you mind posting your code for LoadGameIntoGameGUI ? 如果是这样,您介意为LoadGameIntoGameGUI发布代码吗?

Btw, if here is unnecessary, if GameInfo.NumberOfQuestions==0 the loop will be ignored anyway: 顺便说一句, if这是不必要的,如果GameInfo.NumberOfQuestions==0 ,则无论如何将忽略循环:

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

What are these numbers and questions? 这些数字和问题是什么? You pass one of them into LoadGameIntoGameGUI , are you sure it's correct? 您将其中之一传递给LoadGameIntoGameGUI ,确定正确吗? You can add log-file and inspect the input. 您可以添加日志文件并检查输入。 Something like File.WriteAllText("your log.txt", question.ToString()); 类似于File.WriteAllText("your log.txt", question.ToString()); (make sure ToString returns something meaningful). (确保ToString返回有意义的东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM