tags:

views:

789

answers:

4

Errr... 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, everytime i open a txt file from explorer (doubleclicking 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 fofr this inconvenience. [Send error report] or [Don't send].

When I click on "What does this error report contsin", 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 :P

and... The program itself also does exist, and the computer knows it exists, because it tried to open it.

Another thing... It is NOT possible that the program is trying to open another file on Form_Load, because I didn't make it that way. So, WHAT THE!!!???

Has anybody had this problem before? Can anybody please help?

edit i can open other files by double-clicking them, just not txt files (this was working for a whole month up until yesterday.

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


Edit:

Okay, so I've edited the above code to display the exception message in a messagebox in a try/catch block if it fails. Now, when the mesagebox is displayed, no matter which directory the file is actually contained in, the message is always the same path: "Could not find file C\Documents". First, There is no "Documents" directory in C:\ - Second, there is no code in my app that points to that directory, and third, I think this may be caused by too many spaces in the filepath, because when i move the txt file to a directory that contains no spaces, such as: "C:\myfile.txt", it opens correctly in my text editor. :( The weird thing is that I've never had this problem before, so that makes me think that the arguments for file association or file extensions or something like that is screwed up for txt files...

+9  A: 

Open debugger. Find bug. report here. We help you.

Byron Whitlock
I've been up all day yesterday and all night trying to find a bug, as far as I can see, there is no bug. It doesn't make any sense. If there was a bug, it wouldn't have been working perfectly for the past month.
baeltazor
What happens on the line of code that tries to open the file? What is the path of the file in the debugger?
Byron Whitlock
You must misunderstand what a bug is. If it's not working there's a bug. Is it working?
recursive
@baeltazor: You say: "If there was a bug, it wouldn't have been working perfectly for the past month." And that is where you are wrong. Just because something worked in your test environment doesn't mean it's bug-free.
Daniel Pryden
point taken. thank you @Danial Pryden
baeltazor
You were up all day yesterday and all night trying to find a bug, and you didn't trace the code? Trace the code and let us know what's in args[0] before you try to read from it.
Larry Lustig
+3  A: 

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.

Am
+4  A: 

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.

Michael Petrotta
Agreed. Also, if you build in a catch-everything block and just have it log out to the event log, you can leave it in the production code, and it will provide useful feedback in case you have other failures in the field.
Daniel Pryden
@Daniel: I love logging. Ask anyone on my team :-)
Michael Petrotta
thank you Micahel, i tried your way, and when i try to open the file from explorer, the exception is displayed in a messagebox saying "Could not find file 'C:\Documents'. Even though the file is actually inside: "C:\Documents and Settings\Jason Pezzimenti\My Documents\file.txt" ..so, i'm thinking it has something to do with the filepath having more than one space in it, like Slaks mentioned below?.
baeltazor
Yep, sounds like it. The easiest way to fix this is to fix the file association. Assuming <= WindowsXP, go to Folder Options, File Types, find your file association and click "Advanced", edit the association, and in the textbox under "Application used to perform action", ensure that the %1 has quotes around it. You'll end up with something like: '"c:\program files\myapp.exe" "%1"'
Michael Petrotta
yep, that's what i already had.
baeltazor
What, exactly, is the file association string? Post it here, maybe it'll provide a clue.
Michael Petrotta
thank you heaps @michael for all your help
baeltazor
+24  A: 

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.

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


Alternatively, your program might be referencing non-standard libraries (ones that aren't part of .Net itself), and for some reason cannot find them anymore. If so, it wouldn't run at all, even if you launch it normally. Does it? If so, copy them all to the program's directory. EDIT: You said it doesn't use any.

SLaks
Beat me to it. I was just about to write the same answer.
Daniel Pryden
thank you slaks, i'm pretty convinced that you're right on this, although i'm not sure. i just captured the exception message in a messagebox and it said "Could not find file 'C:\Documents'. Even though the file is actually inside: "C:\Documents and Settings\Jason Pezzimenti\My Documents\file.txt"... weird
baeltazor
thank you so much slaks for your help. i really appreciate it. :D problem solved
baeltazor
@baeltazor: what solution did you select?
Michael Petrotta
With some more upvotes, I can get the gold Reversal badge! :-)
SLaks
@SLaks: you've got a ways to go, but good luck.
Michael Petrotta