• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1804
  • Last Modified:

Problems with Direct / Relative Paths: File not found exception

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!


0
i-Thomas
Asked:
i-Thomas
  • 3
  • 2
2 Solutions
 
WebSpecialsCommented:

yes i ve found the error..

simple:
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
0
 
TheAvengerCommented:
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");
0
 
WebSpecialsCommented:
@Avenger
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TheAvengerCommented:
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.
0
 
TheAvengerCommented:
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
0
 
i-ThomasAuthor Commented:
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...
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now