您的位置:首页 > 编程语言 > C#

C#之DirectoryInfo操作

2015-12-11 16:33 543 查看
在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的包裹。这样更有利于程序的健壮性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: