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);//讀取文件設定
StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312"));//設定讀寫的編碼
//使用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();
}
//關閉此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)容為空無法進行替換,請檢查文件");
}
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);
//關閉此文件
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ù)默認覆蓋
//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);
//按行讀取為字符串數(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)容了,希望大家能夠喜歡。
相關文章
基于C#的socket編程的TCP異步的實現(xiàn)代碼
本篇文章主要介紹了基于C#的socket編程的TCP異步的實現(xiàn)代碼,詳解的講訴了TCP通信異步的實現(xiàn),有興趣的可以了解一下。2016-11-11
automation服務器不能創(chuàng)建對象 解決方法
本文主要介紹如何解決“automation服務器不能創(chuàng)建對象”錯誤,從而解決Visual Studio.Net不能正常使用的問題,需要的朋友可以參考下。2016-06-06

