CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011

    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

    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:

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

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.