2

I'm making an example to write to static file (.txt).

I've tried:

    public IActionResult Index()
    {
        string dt = DateTimeOffset.Now.ToString("ddMMyyyy");

        string path = Path.Combine(_environment.WebRootPath, $"Logs/{dt}");

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        string filePath = Path.Combine(_environment.WebRootPath, $"Logs/{dt}/{dt}.txt");

        using (FileStream fs = System.IO.File.Create(filePath))
        {
            AddText(fs, "foo");
            AddText(fs, "bar\tbaz");
        }

        return View();
    }

    private void AddText(FileStream fs, string value)
    {
        byte[] info = new System.Text.UTF8Encoding(true).GetBytes(value);
        fs.Write(info, 0, info.Length);
    }

But it created everything inside wwwroot folder instead of the root of project.

1

I want to create the text file here:

2

Where can I edit?

UPDATE:

This is how I define _environment:

public class HomeController : Controller
{
    private readonly IHostingEnvironment _environment;

    public HomeController(IHostingEnvironment environment)
    {
        _environment = environment;
    }
}
7
  • Well isn't _environment.WebRootPath giving you the path to wwwroot here? Commented May 23, 2017 at 21:34
  • 1
    replace Logs/{dt} with ../Logs/{dt} Commented May 23, 2017 at 21:38
  • @Lashane Exactly what I'm looking for. Thank you so much! Commented May 23, 2017 at 21:41
  • @Lashane That assumes you are keeping the default wwwroot folder of course. Commented May 23, 2017 at 21:48
  • 1
    @Lashane Or you just use ContentRootPath instead of WebRootPath and avoid any confusion at all. Commented May 23, 2017 at 21:54

2 Answers 2

5

The problem is that you are using IHostingEnvironment.WebRootPath to determine the root folder. This property is the absolute path to the directory that contains the web-servable application content files - i.e. the wwwroot folder. Instead you should use the IHostingEnvironment.ContentRootPath property which will give you the absolute path to the directory that contains the application.

For example:

var contentRoot = _environment.ContentRootPath;
var webRoot = _environment.WebRootPath;

//contentRoot = "C:\Projects\YourWebApp"
//webRoot     = "C:\Projects\YourWebApp\wwwroot"

You could use WebRootPath and navigate to it's parent directory, but if for some reason you choose to move wwwroot somewhere else, your code may break. Much better to use a method that will give you the correct path every time in every project.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for classifying. I've just tested. It's working.
0

Instead of

string filePath = Path.Combine(_environment.WebRootPath, $"Logs/{dt}/{dt}.txt");

use

var folder = new System.IO.DirectoryInfo(_environment.WebRootpath).Parent.FullName;
var filePath = Path.Combine(folder, $"Logs/{dt}/{dt}.txt");

1 Comment

Oops, should be DirectoryInfo. Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.