본문 바로가기

   
Programming/C#

파일 & 디렉토리

반응형

파일 & 디렉토리

 - 파일 및 디렉토리 조작 클래스

 - System.Id 네임스페이스

 - FileSystemInfo 클래스 파생

 - 파일 : FileInfo 클래스

 - 디렉토리 : DirectoryInfo 클래스

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;// InputOutput

 

namespace ConsoleEx

{

       class Ex115_FileInfo

       {

             static void Main(string[] args)

             {

                    //M1();

                    //M2();

                    //M3();

                    //M4();

                    M5();

             }

 

             private static void M5()

             {

                    //이름 바꾸기

                    FileInfo file = new FileInfo(@"D:\김종현\test.txt");

                    if (file.Exists)

                    {

                           //같은 폴더에 동일 파일을 다른 이름 복사

                           //원본 삭제

                           file.MoveTo(@"D:\김종현\copy.txt");

                    }

             }

 


private static void M4()

             {

                    //이동

                    FileInfo file = new FileInfo(@"D:\[01]CSharp\test.txt");

                    if (file.Exists)

                    {

                           //이동 : 복사 -> 원본 삭제

                           //file.CopyTo(@"D:\" + file.Name);

                           //file.Delete();

 

                           //복사, 이동시 파일명을 변경 가능!!!!

                           //file.MoveTo(@"D:\" + file.Name);

 

                           //1. 이동하고자 하는 폴더에 이동하려는 파일명과 같은 파일이 존재하는지?

                           FileInfo temp = new FileInfo(@"D:\" + file.Name);

                           //Console.WriteLine(temp.Exists);

                           if (temp.Exists)

                           {

                                 //덮어쓸지?, 그만둘지?

                                 Console.Write("덮어쓰면 y, 그만둘거면 n 입력 : ");

                                 if (Console.ReadLine() == "y")

                                 {

                                        //덮어쓰기 : 목적지 파일 삭제 -> 원본 이동

                                        temp.Delete();

                                        file.MoveTo(@"D:\" + file.Name);

                                 }

                           }

                           else

                           {

                                 //안전하니까 이동해라..

                                 file.MoveTo(@"D:\" + file.Name);

                           }

                    }

             }

 


private static void M3()

             {

                    //삭제

                    //1. 파일객체생성

                    FileInfo file = new FileInfo(@"D:\[01]CSharp\test.txt");

                    if (file.Exists)

                    {

                           //2. 삭제

                           file.Delete();

                    }

             }

 


private static void M2()

             {

                    //복사

                    //1. 파일객체생성

                    FileInfo file = new FileInfo(@"D:\[01]CSharp\test.txt");

                    //2. 존재?

                    if (file.Exists)

                    {

                           //3. 복사

                           //file.CopyTo(@"D:\test.txt");

                           //file.CopyTo(@"D:\" + file.Name);//**

                           //file.CopyTo(@"D:\" + file.Name, true);

 

                           try

                           {

                                 file.CopyTo(@"D:\" + file.Name);

                           }

                           catch (Exception e)

                           {

                           }

                    }

             }

 


private static void M1()

             {

                    // D:\[01]CSharp\test.txt 조작

 

                    //1. 특정 파일 접근하기위한 FileInfo 객체 생성

                    FileInfo file = new FileInfo(@"D:\김종현\test.txt");

 

                    //2. 실제로 파일이 존재하는지?

                    //Console.WriteLine(file.Exists);

                    if (file.Exists)

                    {

                           //3. 파일의 정보

                           Console.WriteLine(file.Name);

                           Console.WriteLine(file.FullName);

                           Console.WriteLine(file.Extension);

                           Console.WriteLine(file.Length);//바이트

                           Console.WriteLine(file.CreationTime);

                           Console.WriteLine(file.LastAccessTime);

                           Console.WriteLine(file.DirectoryName);

                           Console.WriteLine(file.Attributes);

                           Console.WriteLine(file.LastWriteTime);

                    }

                    else

                    {

                           Console.WriteLine("파일이 존재하지 않습니다.");

                    }

             }

 

 

}
}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;//**

 

namespace ConsoleEx

{

       //Ex116_Directory.cs

       class Ex116_Directory

       {

             static void Main(string[] args)

             {

                    //M1();

                    //M2();

                    int result;

 

                    if (int.TryParse("100", out result))

                    {

                           Console.WriteLine(result);

                    }

                    else

                    {

                           Console.WriteLine("예외");

                    }

                   

                    //M3();

                    //M4();

                    M5();

             }

 

             private static void M5()

             {

                    //디렉토리 이동

                    DirectoryInfo dir = new DirectoryInfo(@"D:\김종현\Test");

                    //dir.MoveTo(@"D:\" + dir.Name);

 

                    //디렉토리 이름바꾸기

                    dir.MoveTo(@"D:\김종현\Rename");

             }

 


private static void M4()

             {

                    //디렉토리 삭제

                    DirectoryInfo dir = new DirectoryInfo(@"D:\AAA");

                    //dir.Delete();

                    //dir.Delete(true);

 

                    //현재 삭제하려는 디렉토리가 자식 디렉토리나 자식 파일을 가졌으면.. 사용자에게 질문?

                    if (dir.GetDirectories().Length > 0 || dir.GetFiles().Length > 0)

                    {

                           //질문 필수!!

                           Console.Write("폴더가 비어있지 않습니다. 정말 삭제하시겠습니까?(y/n) : ");

 

                           if (Console.ReadLine() == "y")

                           {

                                 dir.Delete(true);

                           }

                    }

                    else

                    {

                           //내용이 없으면 무조건 삭제

                           dir.Delete();

                    }

             }

 


private static void M3()

             {

                    //디렉토리 생성

                    //1. 부모폴더가 자식폴더 생성(**)

                    //DirectoryInfo dir = new DirectoryInfo(@"D:\김종현");

                    //이미 존재하면 캔슬

                    //dir.CreateSubdirectory("자식");

                    //dir.CreateSubdirectory(@"A\B\C");

 

 

                    //2. 스스로 생성

                    DirectoryInfo dir = new DirectoryInfo(@"D:\AAA");

                    dir.Create();//현재 dir이 참조하고 있는 경로 폴더를 생성

             }

 

 


private static void M2()

             {

                    //디렉토리 = 자식디렉토리? + 파일?

                    DirectoryInfo dir = new DirectoryInfo(@"D:\김종현\AAA");

 

                    //dir의 자식 디렉토리? *************

                    DirectoryInfo[] sub = dir.GetDirectories();

                    //Console.WriteLine("자식 폴더의 갯수 : " + sub.Length);

                    for (int i = 0; i < sub.Length; i++)

                    {

                           Console.WriteLine(sub[i].Name);

                    }

 

                    //dir의 자식 파일? *************

                    FileInfo[] files = dir.GetFiles();

                    Console.WriteLine(files.Length + "");

 

                    //자식 파일명?

                    foreach (FileInfo file in files)

                    {

                           Console.WriteLine(file.Name);

                    }

             }

 


private static void M1()

             {

                    //정보

                    // D: //X

                    // D:\ //O

                    DirectoryInfo dir = new DirectoryInfo(@"D:\김종현\AA\BBB");

 

                    //폴더 존재?

                    //Console.WriteLine(dir.Exists);

                    if (dir.Exists)

                    {

                           Console.WriteLine(dir.Name);

                           Console.WriteLine(dir.FullName);

                           Console.WriteLine(dir.Root.FullName);

                           Console.WriteLine(dir.Parent.FullName);

                           Console.WriteLine(dir.CreationTime);

                           Console.WriteLine(dir.LastAccessTime);

                           Console.WriteLine(dir.LastWriteTime);

                           Console.WriteLine(dir.Attributes);

                    }

             }

 

 

}
}


반응형