• 点滴积累【C#】---操作文件


    操作文件

    写入文件效果:

      

    写入文件代码:

     1 private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
     2         {
     3             try
     4             {
     5                 FileStream fs = new FileStream(@"D:FileStream.txt", FileMode.Create);
     6                 StreamWriter sw = new StreamWriter(fs);
     7                 string content = textBox1.Text.Trim();
     8                 sw.Write(content);
     9                 sw.Close();
    10                 fs.Close();
    11                 MessageBox.Show("保存成功");
    12             }
    13             catch
    14             {
    15                 MessageBox.Show("保存出错");
    16             }
    17         }

    读取文件效果:

     

    读取文件代码:

     1 private void 查看ToolStripMenuItem_Click(object sender, EventArgs e)
     2         {
     3             try
     4             {
     5                 FileStream fs = new FileStream(@"D:FileStream.txt", FileMode.Open);
     6                 StreamReader sr = new StreamReader(fs);
     7                 string content = sr.ReadToEnd();
     8                 textBox1.Text = content;
     9                 sr.Close();
    10                 fs.Close();
    11 
    12             }
    13             catch
    14             {
    15                 MessageBox.Show("查看出错");
    16             }
    17             
    18         }

    拷贝文件效果:

     

    拷贝文件代码:

     1 private void button1_Click(object sender, EventArgs e)
     2         {
     3             try
     4             {
     5                 string path1 = textBox1.Text.Trim();
     6                 string path2 = textBox2.Text.Trim();
     7                 if (File.Exists(path1) == true)
     8                 {
     9                     File.Copy(path1, path2);
    10                     MessageBox.Show("拷贝成功");
    11                 }
    12             }
    13             catch
    14             {
    15                 MessageBox.Show("拷贝失败");
    16             }
    17         }

    移动文件:

     

    代码:

     1 private void button2_Click(object sender, EventArgs e)
     2         {
     3             try
     4             {
     5                 string path1 = textBox1.Text.Trim();
     6                 string path2 = textBox2.Text.Trim();
     7                 if (File.Exists(path1) == true)
     8                 {
     9                     File.Move(path1, path2);
    10                     MessageBox.Show("移动成功");
    11                 }
    12             }
    13             catch
    14             {
    15                 MessageBox.Show("移动失败");
    16             }
    17         }

    删除文件

    代码:

     1 private void button3_Click(object sender, EventArgs e)
     2         {
     3             try
     4             {
     5                 string path = textBox1.Text.Trim();
     6                 if (File.Exists(path) == true)
     7                 {
     8                     File.Delete(path);
     9                     MessageBox.Show("删除成功");
    10                 }
    11             }
    12             catch
    13             {
    14                 MessageBox.Show("删除失败");
    15             }
    16         }
  • 相关阅读:
    PowerDesigner如何导出建表sql脚本(转)
    excel插入行时提示不能将对象移到工作表外,怎么解决!!
    Axure知识点
    移动互联网学习的点
    什么是大数据?
    [Android开源项目] GitHub开源项目总结 (转)
    程序员自我提高的几点建议 很实诚(转)
    程序员必须进行的10项投资(转)
    安卓版本的问题
    Android APK反编译详解(转)
  • 原文地址:https://www.cnblogs.com/xinchun/p/3436394.html
Copyright © 2020-2023  润新知