System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\MyApp\API\Live\Images\Company Logo.png'

Pratham Jain 221 Reputation points
2023-02-06T10:17:43.6433333+00:00

Hi All,

I am developing an application in .Net Core WebAPI in C#. I need to access company logo and insert it into excel while exporting data to excel. I have added Images folder with logo images to my startup WebAPI project in solution for this purpose.

I am trying to access the logo image with below code:

private readonly IWebHostEnvironment _webHostEnvironment;

workSheet.AddPicture(_webHostEnvironment.ContentRootPath + _configuration.GetSection("CompanyLogoPath").Value).MoveTo(workSheet.Cell(1, 1), workSheet.Cell(5, 5));

I am using webHostEnvironment.ContentRootPath to get the current execution environment path and remaining path comes from the configuration in appsettings.

This code is working fine in dev enviornment but failing on server after deployment with exception in subject.

Please advise how can I resolve this issue ASAP.

Regards,

Pratham

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,077 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 31,756 Reputation points Microsoft Vendor
    2023-02-07T02:23:29.3766667+00:00

    Hi @Pratham Jain

    This code is working fine in dev environment but failing on server after deployment with exception in subject. Could not find a part of the path 'C:\Mapp\API\Live\Images\Company Logo.png'

    Check the C:\MyApp\API\Live\Images\ folder on the server, whether it contains the Company Logo.png? Perhaps the image lost when deploy the application on the server.

    If that is the case, you can copy the Company Logo.png to the C:\MyApp\API\Live\Images\ folder on the server.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments