如何在.Net Core中读/写文件?

时间:2017-01-17 15:24:07

标签: c# file stream .net-core

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

我正在开发我的第一个.Net Core应用程序并寻找 File.Read* / File.Write*个函数(来自System.IO的{​​{1}})备选方案。

7 个答案:

答案 0 :(得分:45)

套餐:System.IO.FileSystem

using (DSIPCDatabaseTableAdapters.QueriesTableAdapter queries = new DSIPCDatabaseTableAdapters.QueriesTableAdapter()) {
            queries.ChangeConnection(DBUtils.GetInstance.GetConnectionString);

            try
            {
                object o = queries.AddUser(textBox1.Text.ToString(), textBox2.Text.ToString(), comboBox1.SelectedValue.ToString());
                if (o != null)
                    showOptionDialog();
                else
                    MessageBox.Show("Insertion Error!");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }
        }

答案 1 :(得分:19)

FileStream fileStream = new FileStream("file.txt", FileMode.Open);
using (StreamReader reader = new StreamReader(fileStream))
{
    string line = reader.ReadLine();
}

使用System.IO.FileStreamSystem.IO.StreamReader。您也可以使用System.IO.BinaryReaderSystem.IO.BinaryWriter

答案 2 :(得分:5)

写:

using (System.IO.StreamWriter file =
new System.IO.StreamWriter(System.IO.File.Create(filePath).Dispose()))
{
    file.WriteLine("your text here");
}

答案 3 :(得分:2)

在Net Core 2.1中工作

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

    string SendData = System.IO.File.ReadAllText(file);

答案 4 :(得分:0)

使用:

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

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

答案 5 :(得分:0)

    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的倍数。在打开源文件和目标文件的同时,它按字节读取大文件并按字节写入新的目标文件。它不会耗尽内存。

答案 6 :(得分:0)

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

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 "";
    }

此处供参考,this是正式的Microsoft文档链接。