==> 텍스트파일저장.cs



using System;
using System.IO; // 파일처리

public class 텍스트파일저장
{
    public static void Main()
    {
        string data = "안녕하세요.\r\n반갑습니다.";

        // StreamWriter 클래스
        StreamWriter sw = new StreamWriter("C:\\Temp\\Test.txt");

        // Write() 메서드 : 저장
        sw.WriteLine(data);

        // StreamWrite 개체를 생성했으면 반드시 닫기
        sw.Close();

        // 메모리 해제
        sw.Dispose();
    }
}




==============================================================================================



==> 텍스트파일읽기.cs



using System;
using System.IO; // 파일처리

public class 텍스트파일읽기
{
    public static void Main()
    {
        StreamReader sr = new StreamReader(@"C:\Temp\Test.txt");
        Console.WriteLine("{0}", sr.ReadToEnd()); // 전체 읽어오기
        sr.Close();
        sr.Dispose();
    }
}



==============================================================================================



==> 파일정보얻기.cs



using System;
using System.IO; //

public class 파일정보얻기
{
    public static void Main()
    {
        string file = "C:\\Temp\\Test.txt";

        // File 클래스 : 정적
        if (File.Exists(file)) // 파일이 있다면
        {
            Console.WriteLine("{0}", File.GetCreationTime(file));
            File.Copy(file, "C:\\Temp\\Test2.txt", true);
        }

        // FileInfo 클래스 : 인스턴스
        FileInfo fi = new FileInfo(file);
        if (fi.Exists) // 파일이 존재한다면,
        {
            Console.WriteLine("{0}", fi.FullName); // 파일명 출력
            // ... ===파일 관련 모든 처리 가능===
        }

    }
}




==============================================================================================



==> 폴더정보얻기.cs



using System;
using System.IO;

public class 폴더정보얻기
{
    public static void Main()
    {
        string dir = "D:\\";

        // Directory 클래스
        if (Directory.Exists(dir))
        {
            // D드라이브의 모든 폴더 목록을 출력
            foreach (string folder in Directory.GetDirectories(dir))
            {
                Console.WriteLine("{0}", folder);
            }
        }

        // DirectoryInfo 클래스
        DirectoryInfo di = new DirectoryInfo(dir + "Temp\\");
        if (di.Exists)
        {
            // D드라이브의 Temp 폴더의 모든 파일 목록 출력
            foreach (var item in di.GetFiles())
            {
                Console.WriteLine("{0}", item);
            }
        }
    }
}

Posted by holland14
: