C#のDirectoryInfo操作


C#のSystem.IOネーミングスペースの下には大量のライブラリがありますが、Directory Infoの使用を見てみましょう.
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の小包に注意.これにより、プログラムの頑丈性が向上します.