Windows窗体编程基础学习:文件读写
2007-03-30 15:01
603 查看
1.[读取]按钮
判断某txt文件是否存在 不存在则新建
读取文件中的内容
2.[写入]按钮
将用户修改后的内容 存入该txt文件
===============================
1.新建专案 及 加入 新建项目 Windows窗体(如txtFileReadWrite)
并设定应用程序的主入口点
Application.Run(new txtFileReadWrite());
2.该窗体加入两个按钮(读取和写入) 及 一个文本框
3.代码部分加入引用
using System.IO;
4.[读取] 按钮 事件 代码
private void btn_txtFileRead_Click(object sender, EventArgs e)
{
try
{
string strDirPath = "E://WinFormStudy//";
string strFilePath = "E://WinFormStudy//myText.txt";
if (File.Exists(strFilePath))
{
//如果存在 则读取其中的文本内容
using (StreamReader sr = new StreamReader(strFilePath))
{
string strLineText = "";
string strAllText = "";
while ((strLineText = sr.ReadLine()) != null)
{
strAllText += strLineText;
}
this.textBox1.Text = strAllText;
}
}
else//如果不存在 则创建
{
if (!Directory.Exists(strDirPath))
{
Directory.CreateDirectory(strDirPath);
}
File.Create(strFilePath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
5.[写入] 按钮 事件 代码
private void btn_txtFileWrite_Click(object sender, EventArgs e)
{
string strFilePath = "E://WinFormStudy//myText.txt";
if (File.Exists(strFilePath))
{
using (StreamWriter sw = new StreamWriter(strFilePath))
{
// 增加一些文件描述
sw.Write("这是新修改后的文件");
sw.Write("修改的时间为: ");
sw.WriteLine(DateTime.Now);
sw.WriteLine("-------------------");
sw.WriteLine(this.textBox1.Text);
}
}
else
{
MessageBox.Show("文件不存在 请先创建");
}
}
判断某txt文件是否存在 不存在则新建
读取文件中的内容
2.[写入]按钮
将用户修改后的内容 存入该txt文件
===============================
1.新建专案 及 加入 新建项目 Windows窗体(如txtFileReadWrite)
并设定应用程序的主入口点
Application.Run(new txtFileReadWrite());
2.该窗体加入两个按钮(读取和写入) 及 一个文本框
3.代码部分加入引用
using System.IO;
4.[读取] 按钮 事件 代码
private void btn_txtFileRead_Click(object sender, EventArgs e)
{
try
{
string strDirPath = "E://WinFormStudy//";
string strFilePath = "E://WinFormStudy//myText.txt";
if (File.Exists(strFilePath))
{
//如果存在 则读取其中的文本内容
using (StreamReader sr = new StreamReader(strFilePath))
{
string strLineText = "";
string strAllText = "";
while ((strLineText = sr.ReadLine()) != null)
{
strAllText += strLineText;
}
this.textBox1.Text = strAllText;
}
}
else//如果不存在 则创建
{
if (!Directory.Exists(strDirPath))
{
Directory.CreateDirectory(strDirPath);
}
File.Create(strFilePath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
5.[写入] 按钮 事件 代码
private void btn_txtFileWrite_Click(object sender, EventArgs e)
{
string strFilePath = "E://WinFormStudy//myText.txt";
if (File.Exists(strFilePath))
{
using (StreamWriter sw = new StreamWriter(strFilePath))
{
// 增加一些文件描述
sw.Write("这是新修改后的文件");
sw.Write("修改的时间为: ");
sw.WriteLine(DateTime.Now);
sw.WriteLine("-------------------");
sw.WriteLine(this.textBox1.Text);
}
}
else
{
MessageBox.Show("文件不存在 请先创建");
}
}
相关文章推荐
- Windows窗体编程基础学习:文件读写
- Windows窗体编程基础学习:更改TabControl 的外观(如qq用的)
- [引]Windows窗体编程基础学习: 对话框组件
- Windows窗体编程基础学习:使用 NotifyIcon 组件向任务栏添加应用程序图标
- Windows窗体编程基础学习: 对话框组件
- Windows窗体编程基础学习:使用 NotifyIcon 组件向任务栏添加应用程序图标
- [引]Windows窗体编程基础学习: 对话框组件
- Windows窗体编程基础学习:使用 NotifyIcon 组件向任务栏添加应用程序图标
- Windows窗体编程基础学习:更改TabControl 的外观(如qq用的)
- LInux 编程基础学习笔记 持续ing 文件读写
- [引]Windows窗体编程基础学习: 对话框组件
- Windows编程基础--窗体篇(这篇文章可能会对你学习WINDOWS编程有些帮助)
- Windows窗体编程基础学习:使用 NotifyIcon 组件向任务栏添加应用程序图标
- DELPHI语法基础学习笔记-Windows 句柄、回调函数、函数重载等(Delphi中很少需要直接使用句柄,因为句柄藏在窗体、 位图及其他Delphi 对象的内部)
- Python基础学习(5)网络编程socket、文件上传、粘包问题、socketserver、IO多路复用、线程与进程、进程池、线程池、上下文管理、协程
- MFC学习笔记2 Windows编程基础--MessageBox
- windows基础编程 - 静态库、动态库文件
- 零基础学习Linux(三)linux与windows文件共享
- 基于Java语言的安卓编程学习之文件读写(上)
- java基础学习之文件读写