您当前的位置: 首页 >  c#

彭世瑜

暂无认证

  • 1浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#编程-110:文件操作File静态类

彭世瑜 发布时间:2017-08-10 23:18:38 ,浏览量:1

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace IOTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //判断文件是否存在
  13.             //file是静态类
  14.             string path = @"C:\Users\pengshiyu\Desktop\新建文本文档.txt";
  15.             if (File.Exists(path)) Console.WriteLine("file \""+path+"\" is exists");
  16.             else Console.WriteLine("file \""+path+"\" is not exists");
  17.  
  18.             //创建文件,注意:需要把创建的文件流关闭
  19.             //方法一:try catch语句
  20.             //方法二:先判断不存在,再创建
  21.             string path1 = @"C:\Users\pengshiyu\Desktop\";
  22.             if (!File.Exists(path1 + "newFile.txt"))
  23.             {
  24.                 FileStream filestream = File.Create(path1 + "newFile.txt");
  25.                 filestream.Close();
  26.                 Console.WriteLine("文件创建成功!");
  27.             }
  28.             else
  29.                 Console.WriteLine("文件已经存在!");
  30.              
  31.             //打开文件
  32.             //FileMode有六种枚举
  33.             string path2 = @"C:\Users\pengshiyu\Desktop\test.txt";
  34.             try
  35.             {
  36.                 FileStream filestream = File.Open(path2,FileMode.Truncate);
  37.                 byte[] writebyte = { (byte)'p', (byte)'s', (byte)'y', (byte)',', (byte)'t', (byte)'e', (byte)'s', (byte)'t' };
  38.                 filestream.Write(writebyte,0,writebyte.Length);
  39.                 filestream.Close();
  40.                 Console.WriteLine("文件写入成功!");
  41.             }
  42.             catch (Exception ex)
  43.             {
  44.  
  45.                 Console.WriteLine(ex.Message);
  46.             }
  47.  
  48.             //文件复制
  49.             string pathSource = @"C:\Users\pengshiyu\Desktop\source\test.txt";
  50.             string pathDestination = @"C:\Users\pengshiyu\Desktop\destination\test.txt";
  51.  
  52.             if (File.Exists(pathSource))
  53.             {
  54.                 try
  55.                 {
  56.                     if (!File.Exists(pathDestination))
  57.                     {
  58.                         Console.WriteLine("请选择复制(1)还是移动(2):");
  59.                         string choice = Console.ReadLine();
  60.                         if (choice == "1")
  61.                         {
  62.                             //文件复制
  63.                             File.Copy(pathSource, pathDestination, false);
  64.                             Console.WriteLine("文件拷贝成功!");
  65.                             Console.WriteLine("是否删除源文件?删除:1,不删除:2");
  66.                             string delChoice = Console.ReadLine();
  67.                             if (delChoice == "1")
  68.                             {
  69.                                 //文件删除
  70.                                 File.Delete(pathSource);
  71.                                 Console.WriteLine("源文件删除成功!");
  72.                             }
  73.                             else if (delChoice == "2")
  74.                             {
  75.                                 Console.WriteLine("不删除!");
  76.                             }
  77.                             else
  78.                             {
  79.                                 Console.WriteLine("用户输入有误!");
  80.                             }
  81.                        }
  82.                         else if (choice == "2")
  83.                         {
  84.                             //文件移动
  85.                             File.Move(pathSource, pathDestination);
  86.                             Console.WriteLine("文件移动成功!");
  87.                         }
  88.  
  89.                         else
  90.                         {
  91.                             Console.WriteLine("文件存在,是否覆盖?是:1,否:2");
  92.                             string choicecover = Console.ReadLine();
  93.                             if (choicecover == "1")
  94.                             {
  95.                                 File.Copy(pathSource, pathDestination, true);
  96.                                 Console.WriteLine("文件拷贝成功,覆盖完成!");
  97.                             }
  98.                             else if (choicecover == "2")
  99.                             {
  100.                                 Console.WriteLine("文件拷贝失败,文件已存在!");
  101.                             }
  102.                             else
  103.                             {
  104.                                 Console.WriteLine("输入有误!");
  105.                             }
  106.  
  107.                         }
  108.                     }
  109.                 }
  110.                 catch (Exception ex)
  111.                 {
  112.  
  113.                     Console.WriteLine(ex.Message);
  114.                 }
  115.             }
  116.             else
  117.             {
  118.                 Console.WriteLine("源文件不存在");
  119.             }
  120.             Console.ReadKey();
  121.         }
  122.     }
  123. }
C#编程-110:文件操作File静态类 C#编程-110:文件操作File静态类 C#编程-110:文件操作File静态类 C#编程-110:文件操作File静态类 C#编程-110:文件操作File静态类 C#编程-110:文件操作File静态类 C#编程-110:文件操作File静态类 C#编程-110:文件操作File静态类
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0612s