본문 바로가기
C#

System.IO.DIrectory

by 캡틴노랑이 2015. 8. 31.
반응형

Directory 관련하여 생성, 삭제, 이동에 관련된 각각의 샘플 코드


CreateDirectory(System.String)

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        string path = @"C:\tijdelijk";

        try
        {
            if (Directory.Exists(path))
            {
                Console.WriteLine("The directory {0} already exists.", path);
            }
            else
            {
                Directory.CreateDirectory(path);
                Console.WriteLine("The directory {0} was created.", path);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }
}

 

 

 

Delete(System.String)

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        string path = @"C:\tijdelijk";

        try
        {
            if (Directory.Exists(path))
            {
                Console.WriteLine("The directory {0} already exists.", path);
            }
            else
            {
                Directory.CreateDirectory(path);
                Console.WriteLine("The directory {0} was created.", path);
            }
            Directory.Delete(path);
            Console.WriteLine("The directory {0} was deleted.", path);
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }
}

 

Delete(System.String, System.Boolean)
using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        string path1 = @"C:\tijdelijk";
        string path2 = @"C:\tijdelijk\subdir";

        try
        {
            if (Directory.Exists(path1))
            {
                Console.WriteLine("The directory {0} already exists.", path1);
            }
            else
            {
                Directory.CreateDirectory(path1);
                Console.WriteLine("The directory {0} was created.", path1);
            }

            if (Directory.Exists(path2))
            {
                Console.WriteLine("The directory {0} already exists.", path2);
            }
            else
            {
                Directory.CreateDirectory(path2);
                Console.WriteLine("The directory {0} was created.", path2);
            }

            Directory.Delete(path1, true);
            Console.WriteLine("The directories {0} and {1} were deleted.", path1, path2);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }

Exists(System.String)

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        string path = @"C:\TEMP";

        if (Directory.Exists(path))
        {
            Console.WriteLine("The path {0} does exist.", path);
        }
        else
        {
            Console.WriteLine("The path {0} does not exist.", path);
        }

        Console.ReadKey();
    }

 

GetCreationTime(System.String)

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        string path = @"C:\tijdelijk";

        try
        {
            if (Directory.Exists(path))
            {
                Console.WriteLine("The directory {0} already exists.", path);
            }
            else
            {
                Directory.CreateDirectory(path);
                Console.WriteLine("The directory {0} was created.", path);
            }

            DateTime dt = Directory.GetCreationTime(path);

            Console.WriteLine("The directory was created at {0} local time.", dt);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }

GetCreationTimeUtc(System.String)

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        string path = @"C:\tijdelijk";

        try
        {
            if (Directory.Exists(path))
            {
                Console.WriteLine("The directory {0} already exists.", path);
            }
            else
            {
                Directory.CreateDirectory(path);
                Console.WriteLine("The directory {0} was created.", path);
            }

            DateTime dt = Directory.GetCreationTimeUtc(path);

            Console.WriteLine("The directory was created at {0} UTC.", dt);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }
}

 

GetCurrentDirectory()

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        try
        {
            Console.WriteLine("The current working directory is {0}.", Directory.GetCurrentDirectory());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }
}

 

GetDirectoryRoot()

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        try
        {
            string path = Directory.GetCurrentDirectory();
            Console.WriteLine("The root of the current working directory is {0}.", Directory.GetDirectoryRoot(path));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }
}

반응형

'C#' 카테고리의 다른 글

Windows Services  (0) 2015.08.31
System.Data.OracleClient 사용하여 Oracle 연결  (0) 2015.08.31
파일 복사 이동  (0) 2015.08.26
File 관련  (0) 2015.08.26
txt 파일 불러오기  (0) 2015.08.26

댓글