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();

              }

  • 相关阅读:
    centos7安装php7
    将centos7镜像源更新为阿里镜像源
    CentOS7 vscode连接本地虚拟机vsftp服务器
    php 查看扩展,配置文件路径命令
    centos查看程序监听的端口
    centos7搭建ftp服务
    redis-事务
    kettle 执行 kjb 临时文件夹 /tmp permission denied 问题
    Spring 声明式事务与编程式事务详解
    进程和线程的区别
  • 原文地址:https://www.cnblogs.com/swarb/p/9924467.html
Copyright © 2011-2022 走看看