如何解决此问题,应用程序显示异常“找不到文件” [英] How do I resolve this problem ,Application showing an exception“file not found ”

查看:97
本文介绍了如何解决此问题,应用程序显示异常“找不到文件”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS2010中使用操作系统WINDOWS XP创建了一个应用程序。

现在,我将操作系统更新到WIN 7并更新了应用程序的位置。

所以,在运行使用打开对话框打开文件的应用程序时,它显示了一些异常,例如找不到文件。

它在WIN XP中工作正常,但现在它显示此错误,如果我们保持bin文件夹中的特定文件工作正常,但如果我们从其他驱动器或文件夹中选择一个文件显示错误。

I created an application in VS2010 with operating system WINDOWS XP.
Now, I updated the os to WIN 7 and also updated the location of the application.
So, while running the application for opening a file using open dialog box it showing some exception like"File Not Found".
It was working fine with WIN XP, but now it showing this error, if we keep that perticular file in bin folder its working fine , but if we choose a file from other drive or a folder it showing error.

<pre lang="c#">
 string chosen_file = "";
    ofd.Title = "Open a file";
    ofd.FileName = "";
    ofd.Filter = "Text Files(*.txt)|*.txt|Rich Text Box(*.rtb)|*.rtb|Word Document(*.doc)|*.doc|HTML Pages(*.htm)|*.html|Cascading Style Sheet(*.css)|*.css|JAVA(*.java)|*.java|video file(*.wmv)|*.wmv|All Files(*.*)|*.*";
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        chosen_file = ofd.FileName;
        // richTextBox2.LoadFile(chosen_file, RichTextBoxStreamType.PlainText);

        var fileInfo = new FileInfo(ofd.FileName);
        fileInfo.Length.ToString();
        byte[] buffer = new byte[fileInfo.Length];

        int length = (int)fileInfo.Length;
        FileStream fileStream = new FileStream(fileInfo.Name, FileMode.Open, FileAccess.Read);
        fileStream.Read(buffer, 0, length);}

推荐答案

FileInfo.Name不包含任何路径信息 - 不像OpenFileDialog.FileName那样。

因此,如果用户选择的文件不在您的应用程序文件夹中(并且它不应该是,你不能在生产中写那个)



你可能意味着:

FileInfo.Name does not include any path information - unlike OpenFileDialog.FileName, which does.
So if the file the user selects is not in your application folder (and it shouldn't be, you can't write there in production)

You probably mean:
FileStream fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);


这篇关于如何解决此问题,应用程序显示异常“找不到文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆