利用C#编写一段读写TXT文本的小程序

在网上看到一段参考的程序如下给出代码:usingSystem;usingSystem.IO;namespaceFile_read{classFileRead{static... 在网上看到一段参考的程序如下

给出代码:using System;
using System.IO;namespace File_read
{
class FileRead
{
static void Main(string[] args)
{
//创建 FileRead 的对象 fr
FileRead fr = new FileRead();
//调用 FileRead 类内的 ReadData 方法
fr.ReadData();
//程序暂停一下,看看结果~
Console.ReadLine(); }
// ReadData 方法
public void ReadData()
{
//创建 FileStream 的对象,说白了告诉程序,文件在那里,对文件如何处理,对文件内容采取的处理方式
FileStream fs = new FileStream("Niit.txt", FileMode.Open, FileAccess.Read);
//仅 对文本 进行 读写操作
StreamReader sr = new StreamReader(fs);
//定位操作点,begin 是一个参考点
sr.BaseStream.Seek(0, SeekOrigin.Begin);
//读一下,看看文件内有没有内容,为下一步循环 提供判断依据
//sr.ReadLine() 这里是 StreamReader的方法 可不是 console 中的~
string str = sr.ReadLine(); //如果 文件有内容
while (str != null)
{
//输出字符串,str 在上面已经定义了 读入一行字符
Console.WriteLine("{0}", str);
//这里我的理解是 当输出一行后,指针移动到下一行~
//下面这句话就是 判断 指针所指这行是否有内容~
str = sr.ReadLine(); }
//关闭文件,注意顺序,先对文件内部进行关闭,然后才是文件~
sr.Close();
fs.Close();
}
}
}但是调试以后调试不通.... 其实自己想学如何编写.有高手能给出简单易于理解的小程序吗?不胜感激! 或者留个QQ号 我想M你想谈,真心想学C#并且在做课程设计.
展开
 我来答
百度网友caef6e2bb
推荐于2016-09-10 · TA获得超过2425个赞
知道小有建树答主
回答量:1174
采纳率:0%
帮助的人:1362万
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <summary>
/// 当某个选项卡被取消选中时
/// </summary>
private void tabControl1_Deselected(object sender, TabControlEventArgs e)
{
e.TabPage.Text = e.TabPage.Text.TrimStart('*');
}

/// <summary>
/// 当某个选项卡被选中时
/// </summary>
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
e.TabPage.Text = "*" + e.TabPage.Text;
}

/// <summary>
/// 打开文件
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//取得当前的选项卡数目
int tabPageCount = this.tabControl1.TabPages.Count;
//取得欲打开的文件路径
string filePath = this.openFileDialog1.FileName;
//取得欲打开的文件名
string fileName = Path.GetFileName(filePath);
//分析是否为txt格式的文件
if (Path.GetExtension(filePath).ToLower() != ".txt")
{
MessageBox.Show("非法文件格式!","打开文件失败",MessageBoxButtons.OK,MessageBoxIcon.Stop);
return;
}
//采用"page"加上当前的选项卡数目加1做为选项卡的名称
string currentPageName = "page" + (tabPageCount + 1).ToString();
//采用"richTextBox"加上当前的选项卡数目加1做为选项卡中包含RichTextBox的名称
string currentTxtName = "richTextBox" + (tabPageCount + 1).ToString();
//添加一个新的选项卡,并指定其选项卡显示文字为当前打开的文件名
this.tabControl1.TabPages.Add(currentPageName,fileName);
//实例化一个新的RichTextBox
RichTextBox ricTxt = new RichTextBox();
//设置其名称,布局等属性
ricTxt.Name = currentTxtName;
ricTxt.Dock = DockStyle.Fill;
//ricTxt.TextChanged +=

//将新这个新的RichTextBox对象添加到当前新建的选项卡中
this.tabControl1.TabPages[currentPageName].Controls.Add(ricTxt);
//将文件加载到当前的RichTextBox
ricTxt.LoadFile(filePath,RichTextBoxStreamType.PlainText);
}
}

private void changePageText(string currentPageName)
{
this.tabControl1.TabPages[currentPageName].Text = "*" + currentPageName;
}
}
}

namespace WindowsApplication2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Location = new System.Drawing.Point(0, -3);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(540, 510);
this.tabControl1.TabIndex = 0;
this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected);
this.tabControl1.Deselected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Deselected);
//
// button1
//
this.button1.Location = new System.Drawing.Point(22, 509);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "浏览";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(539, 540);
this.Controls.Add(this.button1);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
}
}
博思aippt
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT... 点击进入详情页
本回答由博思aippt提供
百度网友f0c02d0
2008-03-11 · TA获得超过317个赞
知道小有建树答主
回答量:388
采纳率:0%
帮助的人:425万
展开全部
Encoding encode = Encoding.GetEncoding("GB2312");//转换中文
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(open.FileName,encode);
string s = reader.ReadLine();//字符型文件是一行行读
while(s != null)
{
richTextBox1.AppendText(s+"\r\n");//尾部\r\n表换行
s=reader.ReadLine();
}
reader.Close();
}

上面这一段放在一个button点击事件里--读文件

下面这一段放在另一个button点击事件里--写文件
StreamWriter writer = new StreamWriter(@"e:\333.txt");
foreach (string s in richTextBox1.Lines)
{
writer.WriteLine(s);
}
MessageBox.Show("保存成功!");
writer.Close();

另外建个richTextBox
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式