Get Solution Folder C#

Asked

Viewed 1,139 times

2

I have a solution with 4 projects inside. In my controller I need to open an HTML that is inside another project, but I can’t open the file folder.

Below follows print with my structure:

inserir a descrição da imagem aqui

The class file is Utils.Cs the controller is within 1 - Services/JWT/Controllers I tried to open the file using the following code...

Criei uma classe no projeto onde está meu arquivo HTML.

public static class UtilDirectory
{

    public static string GetDirectory()
    {
        return Directory.GetCurrentDirectory();
    }

}

In the controller where I do the access class instance as follows:

var directory = UtilDirectory.GetDirectory();

StreamReader reader = new StreamReader(directory + @"/ApiUtils/HtmlEmail/CorpoEmailBoletoDimensao.html");

string mensagem = reader.ReadToEnd();
reader.Close();

But when I do it this way I end up getting the folder of my project that is the controller and not the Html itself.

Can you help me please?

  • I think this little tutorial will help you. https://www.dotnetperls.com/directory-getfiles

  • Which way is coming in directory ? if you call the method GetDirectory() from within a controller, it will return the directory of the controller

  • Due to security restrictions, it is not very recommended to access files outside the scope of a web project directory. Since this file is from another project, could make an API that returns it, instead of searching in the Archive.

  • @Barbetta Exactly, it returns me the path of controller, and because of that I can’t open the Html file.

  • @Tony I’ll try to do it this way.

  • If it works, you tell me that I put that comment of mine as an answer, OK?

  • The correct thing would be for the Htmlemail folder to be in the web application structure or anywhere you want to leave it on the server, but setting up its path in appsettings.json and ensuring that the user running the application has access to the path and files.

  • @Leandroangelo can give me an example of how to configure the path in appsettings.json ? : S

Show 3 more comments

2 answers

1

  • Thanks Roberto. I managed to do otherwise. I ended up forgetting the topic. I will publish the answer as a solution.

0


I fixed the problem by adding a data folder to Solution.

string baseDir = env.ContentRootPath;
AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(baseDir, "App_Data"));

And recovering the folder in my controller for file access as follows.

      string dataDir = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();

      StreamReader reader = new StreamReader(dataDir + @"/CorpoEmailBoletoDimensao.html");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.