Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
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 - Fatal编程技术网

Asp.net core 如何在Asp.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(

我不熟悉.net核心应用程序如何在Asp.net核心应用程序中创建文本文件请提供一些资源

它与经典的.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!");
        }
希望能有帮助

如何在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
用于保存文本文件

试试这个。它应该包含你所需要的一切。