FileNotFound Exception unhandled
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: FileNotFound Exception unhandled

  1. #1
    Join Date
    Apr 2011
    Posts
    9

    FileNotFound Exception unhandled

    Im currently trying to put a picture into my second form when a condition is met on my first form. The code i'm using is:
    secondForm.pictureBox1.Image = Image.FromFile(@"ID.jpeg");

    Now im getting the usual "FileNotFound Exception unhandled" error. I had this issue when taking information from text files before but that was due to poor spelling (sigh). Im getting this error with ID.jpeg but its in the same folder as the text files which now work.

    I should probably check that my "file path exists" but I don't know which folder my program is searching when looking for ID.JPEG! How could I find this out?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,295

    Re: FileNotFound Exception unhandled

    The best way is to supply a full path to the file you want to open.

    If the path to the image files are in a directory relative to the folder
    where the app has been installed, then retrieve the app folder with GetAppFolder sample code from my reply in this post:
    http://www.codeguru.com/forum/showth...t=GetAppFolder

    The use Path.Combine to form the complete path to the file.

    For example, say you have an /Images folder that is a child folder of your app folder which contains the image files.

    Forming the complete relative path to the file, you would end up with:
    Code:
    var idFile = Path.Combine( GetAppFolder( ), "Id.jpeg" );
    secondForm.pictureBox1.Image = Image.FromFile( idFile );
    A word of warning. You can use Environment.CurrentDirectory; however, this isn't reliable because it can change at any time (i.e. any assembly can change this at will, so it may not be what you expect). Using CurrentDirectory is often a source of hard to track down bugs.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center