0
 private static string img = System.Reflection.Assembly.GetExecutingAssembly().Location;
 private static string[] imgs = img.Split(new string[] {"\\ProgramName"},StringSplitOptions.None);
 private  string img_path = imgs[0] + "\\Resources\\LOGO.jpg";//

The above works when debugging on my pc - once I publish the program(I'm going to give it to my wife to use at work) then the path does not match and the image is lost(I need the absolute path because I am using it in a report -formatted in html via code then pushed to pdf).

I think I literally have one problem line:

private string img_path = System.Windows.Forms.Application.StartupPath + "\\Resources\\" + "LOGO.jpg";

Big thanks if you can help! I've tried to load the image itself, but because I am using the path in html - that option doesnt work for me.

  • is it an asp.net or winforms application, where do you publish it? how do you publish it? – Ashkan Mobayen Khiabani Nov 08 '18 at 20:00
  • 1
    Couldn't you store the image as an embedded resource so you could simply read said resource via the location via the solution structure? – Greg Nov 08 '18 at 20:02
  • It is a winforms app. I tried the embedded resource approach but I don't know how to get the path if I do that (only the name) I need the path to build the image into the html. – thomas hungerford Nov 08 '18 at 20:09
  • I publish it as a clickonce - I'm trying on my local machine...the idea is that I would give my wife the install files, she would run the setup and then it would just work. It does minus the image. – thomas hungerford Nov 08 '18 at 20:10
  • Possible duplicate of [Load image from resources area of project in C#](https://stackoverflow.com/questions/1192054/load-image-from-resources-area-of-project-in-c-sharp) – Greg Nov 08 '18 at 20:11
  • I don't need the image I need the path. that other question doesnt work for my situation. – thomas hungerford Nov 08 '18 at 20:16
  • Instead of playing string split games, consider `System.IO.Path.Get​Relative​Path(String, String)`: https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getrelativepath. You might also get something from https://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path – Flydog57 Nov 08 '18 at 20:40
  • yeah - I know - I just tried like 5 things and didn't clean up my code after I got it working. It's bad form I know. thanks though. – thomas hungerford Nov 08 '18 at 20:46

1 Answers1

0

For anybody with a similar problem I ended up solving it myself. I stuck a label on my winforms so that I could print out the location that the published program was running from and figured out which path via Reflection would get me to the right location.

 public string img_path = System.Windows.Forms.Application.StartupPath + "\\Resources\\" + "LOGO.jpg";