0

I am new To Dot-net Core Application How to Create a text File in Asp.net Core Application Please provide Me some resource

1
  • Try this link. It should contain all you need. Commented Apr 24, 2020 at 14:45

2 Answers 2

5

How to Create a text File in Asp.net Core Application

If you'd like to create a text file at specific folder of your ASP.NET Core MVC project, you can refer to the following code snippet.

public class HomeController : Controller
{
    private IHostingEnvironment _env;

    public HomeController(IHostingEnvironment env)
    {
        _env = env;
    }

    public IActionResult Index()
    {
        var path = Path.Combine(_env.ContentRootPath, @"TxtFiles\" + "MyTest.txt");

        using (FileStream fs = System.IO.File.Create(path))
        {
            byte[] content = new UTF8Encoding(true).GetBytes("Hello World");

            fs.Write(content, 0, content.Length);
        }

        //code logic here
        //...

        return View();
    } 

Folder TxtFiles for saving text file(s)

enter image description here

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

Comments

0

it's pretty much the same as the classic .NET

using System;
using System.IO;


        string path= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        using (StreamWriter outputFile = new StreamWriter(Path.Combine(path, "myfile.txt")))
        {
                outputFile.WriteLine("Hello world!");
        }

Hope it helps.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.