![](/img/trans.png)
[英]How to create PID file from ASP.NET Core application under Linux
[英]How to Create a text File in Asp.net Core Application
我是 Dot.net 核心應用程序的新手 如何在 Asp.net 核心應用程序中創建文本文件 請給我一些資源
如何在 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
它與經典的 .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!");
}
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.