Access files in VS - C #

2

Colleagues, I have the following problem: I need to access files of different types (images, pdfs) that are within the solution of my project. I tried to use Server.MapPath but it throws an exception, which mentions that HttpContext.Current is null.

I tried Path.Combine(Environment.CurrentDirectory, @"img\logo.png") and it returns a route pointing to the local disk, but it's not what I need.

Deputy code extract:

if(condicion)
{
   string pathPdf = Path.Combine(Environment.CurrentDirectory, @"img\logo.png");
   emailService.AttachFile(pathPdf);
}

Basically it is a service that sends an email with an attachment when a condition is met.

Any ideas on how to fix it?

    
asked by Paulo Urbano Rivera 15.11.2017 в 16:36
source

1 answer

1

When the code you are running does not run inside the main thread, HttpContext.Current is null and you can not access Server.MapPath . In those cases, a possible solution is to use HttpRuntume.AppDomainAppPath In your case it would be something like:

string path=Path.Combine(HttpRuntime.AppDomainAppPath, "img/logo.png");
    
answered by 15.11.2017 / 16:59
source