//文本文件的创建,和写入数据     引用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();

          }

posted on 2011-11-04 13:38  swarb  阅读(117)  评论(0编辑  收藏  举报