Problems with Direct / Relative Paths: File not found exception

Posted on 2005-04-11
Last Modified: 2007-12-19
Hi Experts!

I have an urgent problem:

I try to load an image with relative path to my Release or after installation with relative path to my Program installation folder, but I always get an System.Reflection.TargetInvocationException in my .exe

public Image Play_n = Image.FromFile(@"\buttons\play_n.jpg");
public Image Play_n = Image.FromFile("\buttons\\play_n.jpg");

both versions do not work.

What can be the reason?

The folder "buttons" is located in the project folder under bin\release\buttons on the same level as my .exe

Do I have to make some additional settings in the IDE so that relative paths are recognized?

What means do I have to detect, which actual path is built from the relative path?

Thank you very much in advance for helping!

Question by:i-Thomas
    LVL 2

    Expert Comment


    yes i ve found the error..

    public Image Play_n = Image.FromFile(@"\buttons\play_n.jpg");

    the path is wrong it shoud be @".\buttons\play_n.jpg"
    because @"\buttons\play_n.jpg" would be absolute (like linux)

    so you can write @".\buttons\play_n.jpg" or @"buttons\play_n.jpg"

    I hope this solves your problem. Ive tested it and it worked for me

    mfg WebSpecials
    LVL 20

    Expert Comment

    The relative paths are taken from the current folder. So you need to change the folder first:

    Directory.CurrentDirectory = Application.StartupPath;
    public Image Play_n = Image.FromFile(@"buttons\play_n.jpg");
    LVL 2

    Assisted Solution

    Ive tried your solution and it doesnt work.
    The right code is:

    Directory.SetCurrentDirectory(Application.StartupPath); //method and not property
    Image my=Image.FromFile(@".\buttons\search.png");

    But after startup the current directory is set to Application.StartupPath. So it doesnt matter.
    But if you want to be sure everything goes right with pathes, use absolute pathes

    By the way. The only way i know to change the current path of an application is to set it in the application.
    So if you dont explicit set CurrentDirectory it isnt changed during execution time.
    (P.S Correct me if i'm wrong)

    mfg WebSpecials
    LVL 20

    Expert Comment

    CurrentDirectory can be another directory. For example make a shortcut and inside the shortcut properties you can give another directory to be the "startup directory". For Windows services the startup directory is always System32.

    Sorry for the property/method mistake.

    Absolute paths are not possible when you have different installations of a program.
    LVL 20

    Accepted Solution

    private void Form1_Load(object sender, System.EventArgs e)
          System.IO.Directory.SetCurrentDirectory (Application.StartupPath);
          Image img = Image.FromFile (@".\buttons\search.png");
          this.pictureBox1.Image = img;

    Works perfectly

    Author Comment

    Hi both of you!

    Thank you very much again for helping me out!!

    I hope my splitting of points is ok for both of you...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now