CodeGo.net>如何在.Net Core中读取/写入文件?

.Net Core中有哪些读取/写入文件的选项?

我正在开发我的第一个.Net Core应用并正在寻找File.Read*/File.Write*功能(.Net中的System.IO)替代品。

Ankit asked 2020-08-07T01:49:09Z
7个解决方案
55 votes

包装:System.IO.FileSystem

System.IO.File.ReadAllText("MyTextFile.txt"); ?
Leszek P answered 2020-08-07T01:49:15Z
22 votes
FileStream fileStream = new FileStream("file.txt", FileMode.Open);
using (StreamReader reader = new StreamReader(fileStream))
{
    string line = reader.ReadLine();
}

使用System.IO.FileStream和System.IO.StreamReader。 您也可以使用System.IO.BinaryReader或System.IO.BinaryWriter。

Biesi Grr answered 2020-08-07T01:49:35Z
5 votes

来写:

using (System.IO.StreamWriter file =
new System.IO.StreamWriter(System.IO.File.Create(filePath).Dispose()))
{
    file.WriteLine("your text here");
}
Chien Dang answered 2020-08-07T01:49:55Z
2 votes

用:

File.ReadAllLines("My textfile.txt");

参考:[https://msdn.microsoft.com/pt-br/library/s2tte0y1(v=vs.110).aspx]

prashanth m answered 2020-08-07T01:50:19Z
1 votes
    public static void Copy(String SourceFile, String TargetFile)
    {

        FileStream fis = null;
        FileStream fos = null;

            try
            {
                Console.Write("## Try No. " + a + " : (Write from " + SourceFile + " to " + TargetFile + ")\n");

                fis = new FileStream(SourceFile, FileMode.Open, FileAccess.ReadWrite);
                fos = new FileStream(TargetFile, FileMode.Create, FileAccess.ReadWrite);

                int intbuffer = 5242880;
                byte[] b = new byte[intbuffer];

                int i;
                while ((i = fis.Read(b, 0, intbuffer)) > 0)
                {
                    fos.Write(b, 0, i);
                }

                Console.Write("Writing file : " + TargetFile + " is successful.\n");

                break;
            }
            catch (Exception e)
            {
                Console.Write("Writing file : " + TargetFile + " is unsuccessful.\n");
                Console.Write(e);
            }
            finally
            {
                if (fis != null)
                {
                    fis.Close();
                }
                if (fos != null)
                {
                    fos.Close();
                }
            }
    }

上面的代码将读取一个大文件并写入一个新的大文件。 可以将“ intbuffer”值设置为1024的倍数。打开源文件和目标文件时,它将按字节读取大文件,然后按字节写入新的目标文件。 它不会耗尽内存。

RotiBlata answered 2020-08-07T01:50:39Z
1 votes

在Net Core 2.1中工作

    var file = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "email", "EmailRegister.htm");

    string SendData = System.IO.File.ReadAllText(file);
AlexGarciaN answered 2020-08-07T01:50:59Z
0 votes

用于将任何文本写入文件。

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文档链接。

Faraz Ahmed answered 2020-08-07T01:51:27Z
translate from https://stackoverflow.com:/questions/41700898/how-to-read-write-files-in-net-core