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