文件操作.txt,.json

一:读写文件

 public static void read(string path)
        {
            StreamReader tx = new StreamReader(path, Encoding.Default);
            string line;
            while ((line = tx.ReadLine()) != null)
            {
                Console.WriteLine(line.ToString());
                Console.ReadKey();
            }
        }
        public static void Write(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write("Hello World!!!!");
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
           
        }
        public static void FileAdd(string Path, string strings)
        {
            StreamWriter sw = File.AppendText(Path);
            sw.Write(strings);
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }

二:删除文件

   private static void DeleteFile(string fileDirect, int saveDay)
        {
            DateTime nowTime = DateTime.Now;

            string[] files = Directory.GetFiles(fileDirect, "*.txt", System.IO.SearchOption.AllDirectories);
            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                TimeSpan t = nowTime - fileInfo.CreationTime;
                int day = t.Days;
                if (day >= saveDay)
                {
                    FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs, RecycleOption.DeletePermanently);
                    Console.WriteLine("");
                }
            }
        }
using Microsoft.VisualBasic.FileIO;//必须引入这个命名空间;先添加引用 Microsoft.VisualBasic.dll
 static void Main(string[] args)
        {
            Console.WriteLine("删除文件到回收站");
            string filepath = "leaver.txt";//文件路径
            FileSystem.DeleteFile(filepath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);//RecycleOption:1.永久删除;2.放到回收站。
            Console.WriteLine("删除文件完成");

            Console.WriteLine("永久移除");
            string filepath_other = "leaver.txt";//文件路径
            FileSystem.DeleteFile(filepath_other, UIOption.OnlyErrorDialogs, RecycleOption.DeletePermanently);
            Console.WriteLine("移除完成");
        }

三:创建,保存,打开文件
(Json格式保存数据)

 
        public void create(string path, string name)
        {
            string var = DateTime.Now.ToString();
            Directory.CreateDirectory( "D:\\路径\\Day3\\log");
            if (System.IO.File.Exists(path + name + ".txt"))
            {
                MessageBox.Show("该文件已存在");
            }
            else
            {
                FileStream fs = new FileStream(path + name + ".txt", FileMode.Create);
                MessageBox.Show("新建成功");
            }
        }
        GlobalVar global = new GlobalVar();
       
        private static void DeleteFile(string fileDirect, int saveDay)
        {
          DateTime nowTime = DateTime.Now;
            string[] files = Directory.GetFiles(fileDirect, "*.txt", System.IO.SearchOption.AllDirectories);
            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                TimeSpan t = nowTime - fileInfo.CreationTime;
                int day = t.Days;
                if (day >= saveDay)
                {
                    FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                    //File.Delete(file);
                }
            }
        }
        public void save()
        {
            if (File.Exists("D:\\路径\\Day3\\log" + DateTime.Now.ToString("yyyy年MM月dd日") + ".txt"))
            {
                DialogResult RESULT = MessageBox.Show("是否确认覆盖原有文件?", "信息提示", MessageBoxButtons.YesNo);
                if (RESULT==DialogResult.Yes)
                {
                    // File.Delete("D:\\2.txt");
                    GlobalVar tmp = new GlobalVar();
                    for (int i = 0; i < ff.dataGridView1.RowCount; i++)
                    {
                        if (ff.dataGridView1.Rows[i].Cells[0].Value == null 
                            || ff.dataGridView1.Rows[i].Cells[1].Value == null
                            || ff.dataGridView1.Rows[i].Cells[2].Value == null
                           || ff.dataGridView1.Rows[i].Cells[3].Value == null
                           || ff.dataGridView1.Rows[i].Cells[4].Value == null
                           || ff.dataGridView1.Rows[i].Cells[5].Value == null)
                            {
                      
                                //MessageBox.Show("请输入正确的格式");
                                continue;                  
                        }
                        
                        Point p = new Point();
                        p.Name = ff.dataGridView1.Rows[i].Cells[0].Value.ToString();
                        p.X = float.Parse(ff.dataGridView1.Rows[i].Cells[1].Value.ToString());
                        p.Y = float.Parse(ff.dataGridView1.Rows[i].Cells[2].Value.ToString());
                        p.Z = float.Parse(ff.dataGridView1.Rows[i].Cells[3].Value.ToString());
                        p.R = float.Parse(ff.dataGridView1.Rows[i].Cells[4].Value.ToString());
                        p.Hand = ff.dataGridView1.Rows[i].Cells[5].Value.ToString();
                       // if(p.Hand==MouseButtons.Left)
                        tmp.listpoint.Add(p);  
                    }
                    save_config_file("D:\\路径\\Day3\\log" + DateTime.Now.ToString("yyyy年MM月dd日") + ".txt", JsonConvert.SerializeObject(tmp));
                    MessageBox.Show("保存成功");
                }
                else
                {
                    return;
                }
            }
            else
            {
                FileStream fs = new FileStream(@"D:\\路径\\Day3\\log" + DateTime.Now.ToString("yyyy年MM月dd日") + ".txt", FileMode.Create);
                fs.Close();
                GlobalVar tmp = new GlobalVar();
                for (int i = 0; i < ff.dataGridView1.RowCount; i++)
                {
                    if (ff.dataGridView1.Rows[i].Cells[0].Value == null
                           || ff.dataGridView1.Rows[i].Cells[1].Value == null
                          || ff.dataGridView1.Rows[i].Cells[2].Value == null
                          || ff.dataGridView1.Rows[i].Cells[3].Value == null
                          || ff.dataGridView1.Rows[i].Cells[4].Value == null)
                        continue;
                    Point p = new Point();
                    p.Name = "p";
                    p.X = float.Parse(ff.dataGridView1.Rows[i].Cells[0].Value.ToString());
                    p.Y = float.Parse(ff.dataGridView1.Rows[i].Cells[1].Value.ToString());
                    p.Z = float.Parse(ff.dataGridView1.Rows[i].Cells[2].Value.ToString());
                    p.R = float.Parse(ff.dataGridView1.Rows[i].Cells[3].Value.ToString());
                    p.Hand = ff.dataGridView1.Rows[i].Cells[4].Value.ToString();
                    tmp.listpoint.Add(p);
                }
                save_config_file("D:\\路径\\Day3\\log" + DateTime.Now.ToString("yyyy年MM月dd日") + ".txt", JsonConvert.SerializeObject(tmp));
                MessageBox.Show("保存成功");
            }
        }
        public void open()
        {

            string lines = string.Empty;
            lines = LoadParameter("D:\\路径\\Day3\\log" + DateTime.Now.ToString("yyyy年MM月dd日") + ".txt");
            global = JsonConvert.DeserializeObject<GlobalVar>(lines);
            ff.dataGridView1.Rows.Clear();
            for (int i = 0; i < global.listpoint.Count; i++)
            {
                ff.dataGridView1.Rows.Add();
                ff.dataGridView1.Rows[i].Cells[0].Value = global.listpoint[i].Name.ToString();
                ff.dataGridView1.Rows[i].Cells[1].Value = (float)global.listpoint[i].X;
                ff.dataGridView1.Rows[i].Cells[2].Value = (float)global.listpoint[i].Y;
                ff.dataGridView1.Rows[i].Cells[3].Value = (float)global.listpoint[i].Z;
                ff.dataGridView1.Rows[i].Cells[4].Value = (float)global.listpoint[i].R;
                ff.dataGridView1.Rows[i].Cells[5].Value = global.listpoint[i].Hand.ToString();
            }

        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值