C# 讀寫txt文件方法


添加引用:

using System.IO;

1.File類寫入文本文件:

private void btnTextWrite_Click(object sender, EventArgs e)
        {
            //文件路徑
            string filePath = @"E:\123\456.txt";

            //檢測文件夾是否存在,不存在則創建
            NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));

            //定義編碼方式,text1.Text為文本框控件中的內容
            byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
            string mystr1 = Encoding.UTF8.GetString(mybyte);

            //寫入文件
            //File.WriteAllBytes(filePath,mybyte);//寫入新文件
            //File.WriteAllText(filePath, mystr1);//寫入新文件
            File.AppendAllText(filePath, mystr1);//添加至文件

        }

2.File類讀取文本文件:

private void btnTexRead_Click(object sender, EventArgs e)
        {
            //文件路徑
            string filePath = @"E:\123\456.txt";
            try
            {
                if (File.Exists(filePath))
                {
                    text1.Text = File.ReadAllText(filePath);
                    byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
                    text1.Text = Encoding.UTF8.GetString(mybyte);
                }
                else
                {
                    MessageBox.Show("文件不存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

3.StreamWrite類寫入文本文件:

private void btnTextWrite_Click(object sender, EventArgs e)
        {
            //文件路徑
            string filePath = @"E:\123\456.txt";

            try
            {
                //檢測文件夾是否存在,不存在則創建
                string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));

                using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
                {
                    byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
                    text1.Text = Encoding.UTF8.GetString(mybyte);
                    sw.Write(text1.Text);
                }

            }
            catch
            {

            }
        }

4.StreamReader類讀取文本文檔:

private void btnTexRead_Click(object sender, EventArgs e)
        {
            //文件路徑
            string filePath = @"E:\123\456.txt";
            try
            {
                if (File.Exists(filePath))
                {
                    using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8))
                    {
                        text1.Text = sr.ReadToEnd();
                        byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
                        text1.Text = Encoding.UTF8.GetString(mybyte);
                    }
                }
                else
                {
                    MessageBox.Show("文件不存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

----------------------------------------------------------------------------------------------

遇到的常見錯誤:

1.ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一個“方法”,這在給定的上下文中無效

解決方法:Controller.File方法和System.IO.File類名稱沖突的問題,只要完整輸入明確類名就可解決。

例如:File.ReadAllText();  改為  System.IO.File.ReadAllText();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM