简体   繁体   中英

My application crashes with a FileNotFoundException, and I don't understand why

I have a text editor I made, which has been working perfectly for the past month without any problems. But today, and all of yesterday, every time I open a txt file from explorer (double clicking it) instead of it opening in my editor, a message appears saying:

Text Editor has encountered a problem and needs to close. We are sorry for this inconvenience. [Send error report] or [Don't send].

When I click on "What does this error report contain", it shows the following:

EventType : clr20r3     P1 : texteditor.exe     P2 : 1.0.0.0     P3 : 4ad32c52     
P4 : mscorlib     P5 : 2.0.0.0     P6 : 492b834a     P7 : 343f     P8 : d8     
P9 : system.io.filenotfoundexception

So that basically tells me that its looking for a file that doesn't exist. But here's my problem:
The file I am trying to open DOES exist because I just double clicked on it

Here is the code that opens a file that has been double clicked on from windows explorer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace TextEditor
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args.Length >= 1)
            {
                Form1 f = new Form1();
                f.txt.Text = System.IO.File.ReadAllText(args[0]);
                f.txt.Tag = args[0];

                Application.Run(f);
            }
            else Application.Run(new Form1());
        }
    }
}

The path you're double-clicking on probably contains one or more spaces, causing the path to be sent as multiple command line arguments.

You need to change the .txt association to send the path in quotes and/or change your app to read all command line arguments and combine them with spaces.

Explorer is sending a command such as

YourApp.exe C:\Documents and Settings\YourName\My Documents\YourFile.txt

Since there aren't any quotes around the string, it's interpreted as 4 different parameters separated by spaces.

You can change the association for .txt files to YourApp.exe "%1" (with the %1 in quotes) to force the entire string to be treated as one argument.

Alternatively, you could replace args[0] with String.Join(" ", args) to put the arguments back together again.

The code you posted doesn't handle any exceptions (like FileNotFoundException) that your program generates. That's why you get the ugly, unhelpful "AppCrash" box. As a debugging step, try wrapping the problematic code in a try/catch block, like so:

try 
{
  if (args.Length >= 1)
{
  // your code
}
catch (Exception e)
{
    Console.WriteLine(e);
}

This will tell you, at least, the method that's failing. Build in debug mode, and run from the command-line with your .pdb file in the same directory, and you'll get the failing line number.

Also, try printing the path you're trying to open (using MessageBox, or Console.WriteLine() from the commandline). You might see something odd. It sounds like you've associated a file type with your application, and are running your app by double-clicking on a file. The shell might be changing the path to that file in a way you don't expect; printing the path will tell you that.

If you're still stuck, post the resulting stack trace. It would also be helpful to post complete, self-contained application code that demonstrates the problem. The code sample you posted is close, but has a dependency on Form1. AU$10 says that in the process of doing this, you'll have a "Eureka" moment and see the problem.

Alternatively, if you have access to a debugger (in Visual Studio), you can step through the code until you see the exception thrown.

Obviously you have a bug: accessing the wrong file.

If you can't debug the error on the given machine, download and use FileMon from sysinernals and see what file is being accessed by you editor.

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