C#文本文件操作

来源:互联网 发布:wifi网络突然变得很差 编辑:程序博客网 时间:2024/05/21 12:45

//文本文件创建、读写、删除

 string path = @"F:\FILE.txt";
            if (!File.Exists(path))
            {
                //创建、写
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("大琳仔");
                }
            }

            //追加
            using (StreamWriter sw = File.AppendText(path))
            {
                sw.WriteLine("小琳仔");
            }

            //读
            string s;
            using (StreamReader sr = File.OpenText(path))
            {
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
            //拷贝
            try
            {
                File.Copy(path,@"F:\F.txt");
            }
            catch (Exception e)
            {
                Console.WriteLine("拷贝失败:" + e.Message);
            }
            //删除
            try
            {
                File.Delete(path);
            }
            catch(Exception e)
            {
                Console.WriteLine("删除失败:"+e.Message);
            }

 

原创粉丝点击