Asp.net core 如何在Asp.net核心应用程序中创建文本文件
我不熟悉.net核心应用程序如何在Asp.net核心应用程序中创建文本文件请提供一些资源它与经典的.net基本相同Asp.net core 如何在Asp.net核心应用程序中创建文本文件,asp.net-core,.net-core,asp.net-core-mvc,asp.net-core-2.1,asp.net-core-mvc-2.1,Asp.net Core,.net Core,Asp.net Core Mvc,Asp.net Core 2.1,Asp.net Core Mvc 2.1,我不熟悉.net核心应用程序如何在Asp.net核心应用程序中创建文本文件请提供一些资源它与经典的.net基本相同 using System; using System.IO; string path= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); using (StreamWriter outputFile = new StreamWriter(Path.Combine(
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!");
}
希望能有帮助
如何在Asp.net核心应用程序中创建文本文件
如果要在ASP.NET Core 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
用于保存文本文件
试试这个。它应该包含你所需要的一切。