操作文件

写入文件效果:

写入文件代码:

 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("删除失败");
}
}

最新文章

  1. PHP array 操作函数
  2. (转)java中静态代码块的用法 static用法详解
  3. CSS第四天总结 更多的属性 圆角 边框图片 段落属性 颜色渐变 盒子阴影
  4. JdbcTemplate queryForMap EmptyResultDataAccessException
  5. 文件泄露&php代码审计
  6. iOS开发 判断用户是否开启了定位
  7. mindmanager 快捷键
  8. (转)select 1 from ... sql语句中的1代表什么意思? .
  9. Swift编程语言学习3.1排列
  10. IIS URL Rewrite Module防盗链规则配置方法
  11. 【Loadrunner】初学Loadrunner——参数化设置(Xml类型)
  12. 学习OpenCV,GPU模块
  13. HDU - 3652
  14. PackageManagerService 学习记录 基于7.1.1源码
  15. Mysql 操作技巧
  16. react的dva框架初试
  17. DevExpress WinForms使用教程:WinForms Sunburst控件
  18. String,StringBuilder,StringBuffer三者的区别(Java)
  19. linux 3.10 的中断收包笔记
  20. 如何在CentOS7上安装Python3及对应问题

热门文章

  1. go排序后索引
  2. html在线美化网站
  3. 定时任务框架-quartz 时间配置
  4. 【JSP EL】<c:if> <c:foreach >EL表达式 获取list长度/不用循环,EL在List中直接获取第一项的内容/EL获取Map的键,Map的值
  5. python中出现 “'gbk' codec can't decode byte 0xf3 in position 20: illegal multibyte sequence”问题
  6. delphi 调用 java
  7. 基于CentOS与VmwareStation10搭建hadoop环境
  8. xss的高级利用
  9. springMVC配置静态资源访问的<mvc:resources>标签的使用
  10. (转) java中try/catch性能和原理