Link to home
Start Free TrialLog in
Avatar of i-Thomas
i-Thomas

asked on

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!


Avatar of WebSpecials
WebSpecials


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
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");
SOLUTION
Avatar of WebSpecials
WebSpecials

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of i-Thomas

ASKER

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