user175084 user175084 - 5 months ago 46
C# Question

Loading image from relative path in Windows Forms

I have an image in my application and i have a picture in my winforms

public static string Correct_Icons = @"C:\Users\xyz\Documents\Visual Studio 2008\Projects\FileShareMgmt\FileShareMgmt\Resources\Correct.png";
public static string warning_Icon = @"C:\Users\xyz\Documents\Visual Studio 2008\Projects\FileShareMgmt\FileShareMgmt\Resources\Warning.png";

cell.Value = Image.FromFile("Resources/warning_Icon);

but i want just want the relative path and not the full path.

eg i want this

public static string Correct_Icons = "\Resources\Correct.png";

and cont.
not working

Answer Source

For my program, Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location) returns C:\code\test\Junk\bin\Debug.

cell.Value = Image.FromFile(
  Path.Combine (
     Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location),

Of course, usually you would embed the resources in your assembly unless you want to change them without a recompile.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download