Как создать текстовый файл в Asp. net Базовое приложение
Если вы хотите создать текстовый файл в указанной c папке вашего ASP. NET Ядро MVC проекта, вы можете обратиться к следующему фрагменту кода.
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();
}
Папка TxtFiles
для сохранения текстовых файлов
