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

              }

  • 相关阅读:
    如何写一个简单的HTTP服务器(重做版)
    如何写一个简单的shell
    Linux守护进程
    字节序:大端法和小端法
    学习计算机需要看哪些经典书?
    IA32寄存器与x86-64寄存器的区别
    C++中extern关键字用法小结
    操作系统中陷阱,中断和异常的区别
    排查CPU占用过高的问题
    git初始化、获取git仓库
  • 原文地址:https://www.cnblogs.com/swarb/p/9924467.html
Copyright © 2020-2023  润新知