C#實現(xiàn)文本文件讀寫方法匯總
更新時間:2015年06月17日 11:26:23 投稿:hebedich
本文給大家匯總介紹了C#實現(xiàn)文本文件讀寫的方法,十分的簡單實用,有需要的小伙伴可以參考下。
方法一:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace txt
{
public partial class Form1 : Form
{
// string path;
public Form1()
{
InitializeComponent();
button3.Click+=button3_Click;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
string path1 = textBox2.Text;
if(!File.Exists(path1))
{
MessageBox.Show("文件不存在");
}
}
//瀏覽按鈕
private void button3_Click(object sender, EventArgs e)
{
/*if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string selUrl = folderBrowserDialog1.SelectedPath;
}*/
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox2.Text = ofd.FileName;
}
//選擇文件夾
/* FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowDialog();
MessageBox.Show(fbd.SelectedPath);
*/
}
//讀取文件
private void button1_Click(object sender, EventArgs e)
{
if(textBox2.Text!=null)
//讀取文件內(nèi)容并顯示在textbox1中讓用戶修改
{
string path=textBox2.Text;
if (File.Exists(path))
// {
// File.Delete(path);
// }
textBox1.Text = File.ReadAllText(path, Encoding.Default);
}
}
private void button2_Click(object sender, EventArgs e)
{
// string[] appendText=textBox1.Text;
File.WriteAllText(textBox2.Text, textBox1.Text, Encoding.Default);
MessageBox.Show("保存成功");
}
}
}
方法二:
namespace 文本文件打開測試
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Read_Click(object sender, EventArgs e)
{
//異常檢測開始
try
{
FileStream fs = new FileStream(@tB_PachFileName.Text , FileMode.Open, FileAccess.Read);//讀取文件設(shè)定
StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312"));//設(shè)定讀寫的編碼
//使用StreamReader類來讀取文件
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
// 從數(shù)據(jù)流中讀取每一行,直到文件的最后一行,并在rTB_Display.Text中顯示出內(nèi)容
this.rTB_Display.Text = "";
string strLine = m_streamReader.ReadLine();
while (strLine != null)
{
this.rTB_Display.Text += strLine + "\n";
strLine = m_streamReader.ReadLine();
}
//關(guān)閉此StreamReader對象
m_streamReader.Close();
}
catch
{
//拋出異常
MessageBox.Show("指定文件不存在");
return;
}
//異常檢測結(jié)束
}
private void btn_Replace_Click(object sender, EventArgs e)
{
//判斷替換開始
if (tB_Replace.Text == ""&&tB_Replace_2.Text=="")
{
MessageBox.Show("想替換的字符都沒有就換啊,你太有才了");
}
else
{
if (rTB_Display.Text == "")
{
MessageBox.Show("文件內(nèi)容為空無法進(jìn)行替換,請檢查文件");
}
else
{
string str = rTB_Display.Text.ToString();
rTB_Display.Text = str.Replace(@tB_Replace.Text ,@tB_Replace_2.Text);//替換
}
}
//結(jié)束
}
private void btn_Save_Click(object sender, EventArgs e)
{
//異常檢測開始
try
{
//創(chuàng)建一個文件流,用以寫入或者創(chuàng)建一個StreamWriter
FileStream fs = new FileStream(@tB_Save.Text, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.Flush();
// 使用StreamWriter來往文件中寫入內(nèi)容
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
// 把richTextBox1中的內(nèi)容寫入文件
m_streamWriter.Write(rTB_Display.Text);
//關(guān)閉此文件
m_streamWriter.Flush();
m_streamWriter.Close();
}
catch
{
//拋出異常
MessageBox.Show("寫入文件失敗,請檢查路徑 文件名與權(quán)限是否符合");
}
//異常檢測結(jié)束
}
}
}
方法三:
//寫入文本文件
class WriteTextFile
{
static void Main()
{
//如果文件不存在,則創(chuàng)建;存在則覆蓋
//該方法寫入字符數(shù)組換行顯示
string[] lines = { "first line", "second line", "third line","第四行" };
System.IO.File.WriteAllLines(@"C:\testDir\test.txt", lines, Encoding.UTF8);
//如果文件不存在,則創(chuàng)建;存在則覆蓋
string strTest = "該例子測試一個字符串寫入文本文件。";
System.IO.File.WriteAllText(@"C:\testDir\test1.txt", strTest, Encoding.UTF8);
//在將文本寫入文件前,處理文本行
//StreamWriter一個參數(shù)默認(rèn)覆蓋
//StreamWriter第二個參數(shù)為false覆蓋現(xiàn)有文件,為true則把文本追加到文件末尾
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\testDir\test2.txt",true))
{
foreach (string line in lines)
{
if (!line.Contains("second"))
{
file.Write(line);//直接追加文件末尾,不換行
file.WriteLine(line);// 直接追加文件末尾,換行
}
}
}
}
}
//讀取文本文件
class ReadTextFile
{
static void Main()
{
//直接讀取出字符串
string text = System.IO.File.ReadAllText(@"C:\testDir\test1.txt");
Console.WriteLine(text);
//按行讀取為字符串?dāng)?shù)組
string[] lines = System.IO.File.ReadAllLines(@"C:\testDir\test.txt");
foreach (string line in lines)
{
Console.WriteLine(line);
}
//從頭到尾以流的方式讀出文本文件
//該方法會一行一行讀出文本
using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\testDir\test.txt"))
{
string str;
while ((str = sr.ReadLine()) != null)
{
Console.WriteLine(str);
}
}
Console.Read();
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
C# OpenCvSharp實現(xiàn)去除文字中的線條
這篇文章主要為大家詳細(xì)介紹了C#如何使用OpenCvSharp實現(xiàn)去除文字中的線條效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
C#實現(xiàn)讀取匿名對象屬性值的方法示例總結(jié)
這篇文章主要介紹了C#實現(xiàn)讀取匿名對象屬性值的方法,結(jié)合實例形式總結(jié)分析了C#通過反射、轉(zhuǎn)換等方法讀取匿名對象屬性值的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
Unity3D啟動外部程序并傳遞參數(shù)的實現(xiàn)
這篇文章主要介紹了Unity3D啟動外部程序并傳遞參數(shù)的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
C# WinForm程序處理后臺繁忙導(dǎo)致前臺控件假死現(xiàn)象解決方法
這篇文章主要介紹了C# WinForm程序處理后臺繁忙導(dǎo)致前臺控件假死現(xiàn)象解決方法,本文通過Application.DoEvents()解決這個問題,并講解了Application.DoEvents()的作用,需要的朋友可以參考下2015-06-06

