How to access a subfolder within a asp.net (C#) Project, WebApi

Viewed 46

I have a seemingly basic question, but I can't access a folder inside my project (actually, it is a project that is one of the references of the main project), which is a folder called "pdfs" that contains 1 PDF that I want to convert to base64.

I put the "pdfs" folder in the same level of the file from where I call it, as I thought it would be easier to access (actionDB.cs file, which is in services/actions/actionDB.cs). The "pdfs" folder is in services/actions/pdfs).

But there is no way to get the correct path of this folder.

On the contrary, if I put the folder in c: , getting path = "c:\\pdfs", and the pdf file there, I can easily find it and convert the pdf perfectly in the code.

Any idea how to access these folders that are inside the projects, as if they were assets? I already put 'copy always: to the destination' and 'type of compilation: content' in the pdf properties, but I still can't access it.

I have already tried the following options:

string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"pdfs");
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"services\actions\pdfs");
string path = Path.Combine(Path.GetDirectoryName(Environment.CurrentDirectory), @"services\actions\pdfs");

The namespace of the actionsDB is ProjectName.Data.services.actions.

I got results such as IIS/.., or other paths inside VS installation…, Assembly and some weird hashes.

Nothing works...

Could you help me with this?

Thank you very much,

CBD

1 Answers

AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose location is relative to the application install directory.

Related