在net core当中可以使用以下方法来读写文本文件首先是读取文本文件

 String basePath = AppContext.BaseDirectory;
            string filePath = Path.Combine(basePath, "conf.txt");

            if (System.IO.File.Exists(filePath))
            {
                FileStream fileStream = new FileStream(filePath, FileMode.Open);
                using (StreamReader reader = new StreamReader(fileStream))
                {
                    string content = reader.ReadToEnd();
                }
            }

写入文件的示例代码如下:

  String basePath = AppContext.BaseDirectory;
            string filePath = Path.Combine(basePath, "conf.txt");
string content = "my content";
  using (System.IO.StreamWriter file =new System.IO.StreamWriter(filePath))
            {
                file.Write(content);
            }

方法不止一种,以上就是最常的方法。下面还再介绍一种最最简单的方法

  String basePath = AppContext.BaseDirectory;
string path = Path.Combine(basePath, "conf.txt");
// 读取方法在这里
string data = System.IO.File.ReadAllText(path, Encoding.UTF8);

// 写入方法在这里
System.IO.File.WriteAllText(path,data+"_new" );

学无止境,今天你get到了吗?文件来源:https://lebang2020.cn/details/210312tflbdkus.html