File

using System;
using System.Collections.Generic;
using System.IO;

namespace File.Test
{
 class MainClass
 {
  
  public static void Main(string[] args)
  {
   while(true)
   {
   Console.WriteLine("请输入要创建的文件文包含其扩展名");
   string input=Console.ReadLine();
   if(input.Length!=0)
   {
   string path="F://C#-test//File.Test//"+input;
   if(!System.IO.File.Exists(path))
   {
    Console.WriteLine("文件{0}不存在",input);
    Console.WriteLine("创建文件{0},Enter Y",input);
    Console.WriteLine("离开!,Enter Exit");
    string input2=Console.ReadLine();
    //Create a file to write to.
    if(input2.Equals("Y")||input2.Equals("y"))
    {
     switch((input.Substring(input.LastIndexOf(".")+1)).ToUpper())
     {
      case "TXT":
       {
        using(StreamWriter sw=System.IO.File.CreateText(path))
        {
         sw.WriteLine("Hello");
         sw.WriteLine("And");
         sw.WriteLine("Welcome");
        }
        break;
       }
      case "ZIP":
       {
        Stream s=System.IO.File.Create(path);
        s.Dispose();
        break;
       }
     }
    }
    if(input2.Equals("Exit")||input2.Equals("exit"))
    {
     break;
    }
   
   }
   if(System.IO.File.Exists(path))
   {
    
    Console.WriteLine("文件{0}已经存在",input);
    Console.WriteLine("内容是:/n");
    Console.WriteLine("文件创建时间:{0}",System.IO.File.GetCreationTime(path));
    Console.WriteLine("文件属性:{0}",System.IO.File.GetAttributes(path));
    try
    {
     using(StreamReader sr=System.IO.File.OpenText(path))
     {
      string s="";
      while((s=sr.ReadLine())!=null)
      {
       Console.WriteLine(s);
      }
     }
    }
    catch(System.IO.IOException e)
    {
     Console.WriteLine("{0}:The write operation could not"+
                      "be performed because the specified"+
                      "part of the file is locked.",e.GetType().Name);
    }
    Console.ReadLine();
   }
   }
   else
   {
    Console.WriteLine("文件名不能为空!!");
    Console.WriteLine();
   }
   }
  }
  
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值