点滴积累【C#】---操作文件
2024-09-22 07:31:23
操作文件
写入文件效果:
写入文件代码:
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream(@"D:\FileStream.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
string content = textBox1.Text.Trim();
sw.Write(content);
sw.Close();
fs.Close();
MessageBox.Show("保存成功");
}
catch
{
MessageBox.Show("保存出错");
}
}
读取文件效果:
读取文件代码:
private void 查看ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream(@"D:FileStream.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string content = sr.ReadToEnd();
textBox1.Text = content;
sr.Close();
fs.Close(); }
catch
{
MessageBox.Show("查看出错");
} }
拷贝文件效果:
拷贝文件代码:
private void button1_Click(object sender, EventArgs e)
{
try
{
string path1 = textBox1.Text.Trim();
string path2 = textBox2.Text.Trim();
if (File.Exists(path1) == true)
{
File.Copy(path1, path2);
MessageBox.Show("拷贝成功");
}
}
catch
{
MessageBox.Show("拷贝失败");
}
}
移动文件:
代码:
private void button2_Click(object sender, EventArgs e)
{
try
{
string path1 = textBox1.Text.Trim();
string path2 = textBox2.Text.Trim();
if (File.Exists(path1) == true)
{
File.Move(path1, path2);
MessageBox.Show("移动成功");
}
}
catch
{
MessageBox.Show("移动失败");
}
}
删除文件
代码:
private void button3_Click(object sender, EventArgs e)
{
try
{
string path = textBox1.Text.Trim();
if (File.Exists(path) == true)
{
File.Delete(path);
MessageBox.Show("删除成功");
}
}
catch
{
MessageBox.Show("删除失败");
}
}
最新文章
- PHP array 操作函数
- (转)java中静态代码块的用法 static用法详解
- CSS第四天总结 更多的属性 圆角 边框图片 段落属性 颜色渐变 盒子阴影
- JdbcTemplate queryForMap EmptyResultDataAccessException
- 文件泄露&;php代码审计
- iOS开发 判断用户是否开启了定位
- mindmanager 快捷键
- (转)select 1 from ... sql语句中的1代表什么意思? .
- Swift编程语言学习3.1排列
- IIS URL Rewrite Module防盗链规则配置方法
- 【Loadrunner】初学Loadrunner——参数化设置(Xml类型)
- 学习OpenCV,GPU模块
- HDU - 3652
- PackageManagerService 学习记录 基于7.1.1源码
- Mysql 操作技巧
- react的dva框架初试
- DevExpress WinForms使用教程:WinForms Sunburst控件
- String,StringBuilder,StringBuffer三者的区别(Java)
- linux 3.10 的中断收包笔记
- 如何在CentOS7上安装Python3及对应问题
热门文章
- go排序后索引
- html在线美化网站
- 定时任务框架-quartz 时间配置
- 【JSP EL】<;c:if>; <;c:foreach >;EL表达式 获取list长度/不用循环,EL在List中直接获取第一项的内容/EL获取Map的键,Map的值
- python中出现 “&#39;gbk&#39; codec can&#39;t decode byte 0xf3 in position 20: illegal multibyte sequence”问题
- delphi 调用 java
- 基于CentOS与VmwareStation10搭建hadoop环境
- xss的高级利用
- springMVC配置静态资源访问的<;mvc:resources>;标签的使用
- (转) java中try/catch性能和原理