我的应用程序崩溃,出现FileNotFoundException,我不明白为什么 [英] My application crashes with a FileNotFoundException, and I don't understand why
问题描述
我有一个文本编辑器,我做了,这已经在过去一个月的工作完全没有任何问题。但今天,一切昨日,每次我打开从资源管理器中一个txt文件(双击它),而不是在我的编辑器中打开,将出现一条消息说:
文本编辑器遇到问题,需要关闭。我们对这个深表歉意
。 [发送错误报告]或[不发送。
块引用>
当我点击这是什么错误报告包含时,显示以下内容:
事件类型: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
所以,基本上告诉我,它在寻找不存在的文件。但这里是我的问题:结果
中的文件我试图打开确实存在,因为我只要双击就可以了。
下面是点击打开一个代码文件已被双从Windows资源管理点击:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;使用System.Windows.Forms的
;
命名空间文本编辑
{
静态类节目
{
///<总结>
///的主入口点应用程序。
///< /总结>
[STAThread]
静态无效的主要(字串[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
如果(args.Length> = 1)
{
Form1中F =新Form1的();
f.txt.Text = System.IO.File.ReadAllText(参数[0]);
f.txt.Tag = ARGS [0];
Application.Run(F);
}
,否则Application.Run(新Form1的());
}
}
}
解决方案你双击上的路径可能包含一个或多个空格,导致路径发送多重命令行参数。
您需要修改
.TXT
关联发送路径报价和/或更改您的应用程序读取所有命令行参数,并用空格将它们结合起来。
浏览器发送一个命令,例如
YourApp.exe C:\Documents和Settings\ YourName\My Documents\YourFile.txt
由于有不是字符串周围的任何报价,它解释为用空格隔开4个不同的参数。
您可以为
.TXT
文件的关联,更改为YourApp.exe%1
(用%1
在引号),以强制被视为一个参数整个字符串。
另外,你可以替换
ARGS [0]
与的string.join(,参数)
将这些参数重新结合在一起。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 itHere 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 toYourApp.exe "%1"
(with the%1
in quotes) to force the entire string to be treated as one argument.Alternatively, you could replace
args[0]
withString.Join(" ", args)
to put the arguments back together again.这篇关于我的应用程序崩溃,出现FileNotFoundException,我不明白为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!