简体   繁体   中英

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. 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:

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.

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. 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. It

looks to me like the file's path is being relayed to "OpenTheFile()" correctly. And because the program is running outside Visual Studio, I get no error messages, not even from the operating system.

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. 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 ?

Btw, if here is unnecessary, if GameInfo.NumberOfQuestions==0 the loop will be ignored anyway:

 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? You can add log-file and inspect the input. Something like File.WriteAllText("your log.txt", question.ToString()); (make sure ToString returns something meaningful).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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