1

Hi all i have image that i am trying to access that is having path below,

C:\migration-apidata\API\API\DesignHub\Images\img-min.png

with the below code

string fileName = AppDomain.CurrentDomain.BaseDirectory + "//Images//img-min.png";
ImagePart imagePart = footerPart.AddImagePart(ImagePartType.Png);
using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); // getting exception here 
imagePart.FeedData(stream);

I am not sure where i am doing wrong while accessing the file path and i have set the image properties like as below image

enter image description here

Could anyone please let me know where I am doing wrong?

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • What is the type of a thrown exception? – Serafim Prozorov Aug 07 '20 at 23:16
  • Print AppDomain.CurrentDomain.BaseDirectory and compare with C:\migration-apidata\API\API\DesignHub – Mate Aug 07 '20 at 23:17
  • When you debug this, what is the value of `fileName`? Have you checked that the file is going where you expect it? – Flydog57 Aug 07 '20 at 23:21
  • Just change the build action in the properties from "None" to "Content". – Serafim Prozorov Aug 07 '20 at 23:22
  • Here you can find a lot of information about build actions: https://stackoverflow.com/questions/145752/what-are-the-various-build-action-settings-in-visual-studio-project-properties – Serafim Prozorov Aug 07 '20 at 23:24
  • the path coming as like this `C:\migration-apidata\API\API\bin\Debug\netcoreapp3.1\//DesignHub//Images//img-min.png` for this code `string fileName = AppDomain.CurrentDomain.BaseDirectory + "//DesignHub//Images//img-min.png";` – Glory Raj Aug 07 '20 at 23:31
  • @Flydog57 the filename coming as this one `C:\migration-apidata\API\API\bin\Debug\netcoreapp3.1\//DesignHub//Images//img-min.png` – Glory Raj Aug 07 '20 at 23:33
  • @SerafimProzorov i changed "None" to "Content" for image properties – Glory Raj Aug 07 '20 at 23:37
  • 1
    Do you see your problem, you have a slash versus backslash typo. You typed `"//Images//img-min.png"` in your code, when you meant to type `"\\Images\\img-min.png"`. Consider using `@"\Images\img-min.png"`; it's less error-prone. Also consider using `System.IO.Path.Combine`, it understands trailing backslashes and other common issues – Flydog57 Aug 07 '20 at 23:37

0 Answers0