您的位置:首页 > 编程语言 > C#

C#文本文件操作

2013-10-08 20:50 190 查看
//文本文件创建、读写、删除

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

            }

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: