Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
<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);}
Posted
Comments
Kornfeld Eliyahu Peter 22-Jul-14 9:20am    
Can you show on what line and what file?

1 solution

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:
C#
FileStream fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
 
Share this answer
 
Comments
OriginalRocky 23-Jul-14 1:02am    
thank you @OriginalGriff, its working.
OriginalGriff 23-Jul-14 3:31am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900