C#编程-110:文件操作File静态类_彭世瑜_新浪博客

举报
彭世瑜 发表于 2021/08/14 01:32:48 2021/08/14
【摘要】 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace IOTest {     cla...
  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静态类


文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/109661863

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。