在C#中的System.IO命名空间下有大量的库供我们使用,下面一起来看一下DirectoryInfo的使用吧。
code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
String temppath = Path.GetTempFileName();
try{
DirectoryInfo directoryInfo = new DirectoryInfo(temppath);
if (directoryInfo.Exists)
{
Console.WriteLine("The directory already exists!");
}
else
{
directoryInfo.Create();
Console.WriteLine("The directory was successfully created!");
directoryInfo.Delete();
Console.WriteLine("The directory was deleted!");
}
}catch(Exception e){
Console.WriteLine(e.Message);
}
}
}
}
总结: 在使用的时候注意要进行try-catch的包裹。这样更有利于程序的健壮性。