zoukankan      html  css  js  c++  java
  • C# FILE文件操作小结

    //文本文件的创建,和写入数据     引用system.io

            string path = @"D:\backup\pg_backup.txt";
                if (!File.Exists(path))
                {
                    FileInfo myfile = new FileInfo(path);
                    FileStream fs = myfile.Create();
                    fs.Close();
                }
                FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs2);
                fs2.SetLength(0);//首先把文件清空。
                string str = saveType + ";" + saveDay.Replace("号", "").ToString() + ";" + saveMonth + ";" + saveYear;
                sw.Write(str);//写你的字符串。
                sw.Close();
                fs2.Close();
                MessageBox.Show("设定成功!");

    //删除文本文件

                            string path = dataGridView1.CurrentRow.Cells[2].Value.ToString().Replace(",", "\\");
                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }

    //读取文本内容

                string path = @"D:\backup\pg_backup.txt";
                if (File.Exists(path))
                {
                    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);
                    string saveInfo = sr.ReadToEnd().ToString();
                    sr.Close();
                    fs.Close();

              }

  • 相关阅读:
    (笔记)Mysql命令mysqldump:备份数据库
    (笔记)Mysql命令rename:修改表名
    (笔记)Mysql命令alter add:增加表的字段
    (笔记)Mysql命令update set:修改表中的数据
    (笔记)Mysql命令delete from:删除记录
    (笔记)Mysql命令select from:查询表中的数据(记录)
    psutil库
    生成器 yield
    高阶函数map(),filter(),reduce()
    logging模块
  • 原文地址:https://www.cnblogs.com/swarb/p/9924467.html
Copyright © 2011-2022 走看看