方法一:
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) //读取文件内容并显示在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); // 从数据流中读取每一行,直到文件的最后一行,并在rTB_display.Text中显示出内容 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; } //异常检测结束 } 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("文件内容为空无法进行替换,请检查文件"); } else { string str = rTB_display.Text.ToString(); rTB_display.Text = str.Replace(@tB_Replace.Text,@tB_Replace_2.Text);//替换 } } //结束 } private voID btn_Save_Click(object sender,EventArgs e) { //异常检测开始 try { //创建一个文件流,用以写入或者创建一个StreamWriter fileStream fs = new fileStream(@tB_Save.Text,fileMode.OpenorCreate,fileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.Flush(); // 使用StreamWriter来往文件中写入内容 m_streamWriter.BaseStream.Seek(0,SeekOrigin.Begin); // 把richTextBox1中的内容写入文件 m_streamWriter.Write(rTB_display.Text); //关闭此文件 m_streamWriter.Flush(); m_streamWriter.Close(); } catch { //抛出异常 MessageBox.Show("写入文件失败,请检查路径 文件名与权限是否符合"); } //异常检测结束 } } }
方法三:
//写入文本文件 class WriteTextfile { static voID Main() { //如果文件不存在,则创建;存在则覆盖 //该方法写入字符数组换行显示 string[] lines = { "first line","second line","third line","第四行" }; System.IO.file.Writealllines(@"C:\testDir\test.txt",lines,EnCoding.UTF8); //如果文件不存在,则创建;存在则覆盖 string strTest = "该例子测试一个字符串写入文本文件。"; System.IO.file.WriteallText(@"C:\testDir\test1.txt",strTest,EnCoding.UTF8); //在将文本写入文件前,处理文本行 //StreamWriter一个参数默认覆盖 //StreamWriter第二个参数为false覆盖现有文件,为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); //按行读取为字符串数组 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(); } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
总结以上是内存溢出为你收集整理的C#实现文本文件读写方法汇总全部内容,希望文章能够帮你解决C#实现文本文件读写方法汇总所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)