C# 如何在.Net Core中读/写文件?
在.Net Core中读/写文件有哪些选项 我正在开发我的第一个.Net核心应用程序并寻找C# 如何在.Net Core中读/写文件?,c#,file,stream,.net-core,C#,File,Stream,.net Core,在.Net Core中读/写文件有哪些选项 我正在开发我的第一个.Net核心应用程序并寻找 File.Read*/File.Write*函数(System.IO来自.Net)选项 FileStream fileStream = new FileStream("file.txt", FileMode.Open); using (StreamReader reader = new StreamReader(fileStream)) { string line = reader.ReadLin
File.Read*
/File.Write*
函数(System.IO
来自.Net
)选项
FileStream fileStream = new FileStream("file.txt", FileMode.Open);
using (StreamReader reader = new StreamReader(fileStream))
{
string line = reader.ReadLine();
}
使用和。您也可以使用或。软件包:
使用:
参考资料:要编写:
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(System.IO.File.Create(filePath).Dispose()))
{
file.WriteLine("your text here");
}
上面的代码将读取一个大文件并写入一个新的大文件。“intbuffer”值可以设置为1024的倍数。当源文件和目标文件都打开时,它按字节读取大文件,并按字节写入新的目标文件。它不会耗尽内存。用于将任何文本写入文件
public static void WriteToFile(string DirectoryPath,string FileName,string Text)
{
//Check Whether directory exist or not if not then create it
if(!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
string FilePath = DirectoryPath + "\\" + FileName;
//Check Whether file exist or not if not then create it new else append on same file
if (!File.Exists(FilePath))
{
File.WriteAllText(FilePath, Text);
}
else
{
Text = $"{Environment.NewLine}{Text}";
File.AppendAllText(FilePath, Text);
}
}
用于从文件中读取文本
public static string ReadFromFile(string DirectoryPath,string FileName)
{
if (Directory.Exists(DirectoryPath))
{
string FilePath = DirectoryPath + "\\" + FileName;
if (File.Exists(FilePath))
{
return File.ReadAllText(FilePath);
}
}
return "";
}
这里有官方的microsoft文档链接供参考。适用于Net Core 2.1
var file = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "email", "EmailRegister.htm");
string SendData = System.IO.File.ReadAllText(file);
请注意,似乎没有FileStream.Lock/.unlock将整个文件读入数组,然后处理数据,而不是在读入数据时处理每一行,速度要快得多。有关更多信息,请参阅本文:@ChrisHansen,这是关于数据异步处理的,而不是阅读…优秀的简短回答,StreamReader在阅读Chris指出的文本时速度最快,你也可以在这里完整地发表一篇关于如何读取.NET Core/.NET上的文件的文章:这是一个关于.NET Core的问题,而不是一个关于.NET框架的参考。在
System.IO.File.Create(filePath)
上可能存在内存泄漏。你能给我一个链接或一些原因@ManIkWeet吗?这是MS的标准教程。System.IO.File.Create(filePath)
创建一个FileStream
,它实现了IDisposable
。我不相信处理StreamWriter
也会处理它所包装的FileStream
。当查看反编译代码时,我可以看到它在包装的流上调用Close()
,而不是Dispose()
。当然,垃圾收集器可能会在一个Finalize()
操作上处理包装的流,但这可能会对性能产生意外影响。我明白了,它应该是System.IO.File.Create(filePath).dispose()
。你同意吗?我只需要使用两个语句,比如:使用(var fileStream=System.IO.File.Create(filePath))使用(var fileWriter=new System.IO.StreamWriter(fileStream)){fileWriter.WriteLine(“此处的文本”);}
删除中断;
public static void WriteToFile(string DirectoryPath,string FileName,string Text)
{
//Check Whether directory exist or not if not then create it
if(!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
}
string FilePath = DirectoryPath + "\\" + FileName;
//Check Whether file exist or not if not then create it new else append on same file
if (!File.Exists(FilePath))
{
File.WriteAllText(FilePath, Text);
}
else
{
Text = $"{Environment.NewLine}{Text}";
File.AppendAllText(FilePath, Text);
}
}
public static string ReadFromFile(string DirectoryPath,string FileName)
{
if (Directory.Exists(DirectoryPath))
{
string FilePath = DirectoryPath + "\\" + FileName;
if (File.Exists(FilePath))
{
return File.ReadAllText(FilePath);
}
}
return "";
}
var file = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "email", "EmailRegister.htm");
string SendData = System.IO.File.ReadAllText(file);