5. DotNetNote.Dul 프로젝트에서 마우스 오른쪽 버튼을 클릭 후 추가 > 클래스를 클릭해 FileUtility.cs라는 이름으로 클래스 파일을 생성하고, 다음과 같이 코드를 작성한다.

    ▼  FileUtility.cs

    using System.IO;
    
    /// <summary>
    /// DotNetNote.Dul.dll: Development Utility Library
    /// </summary>
    namespace DotNetNote.Dul
    {
      /// <summary>
      /// 파일 처리 관련 기본 유틸리티
      /// </summary>
      public class FileUtility
      {
          #region 중복된 파일명 뒤에 번호 붙이는 메서드: GetFileNameWithNumbering
          /// <summary>
          /// GetFilePath: 파일명 뒤에 번호 붙이는 메서드
          /// </summary>
          /// <param name=“dir”>경로(c:\MyFiles)</param>
          /// <param name=“name”>Test.txt</param>
          /// <returns>Test(1).txt</returns>
          public static string GetFileNameWithNumbering(string dir, string name)
          {
              // 순수 파일명: Test
              string strName = Path.GetFileNameWithoutExtension(name);
              // 확장자: .txt
              string strExt = Path.GetExtension(name);
              bool blnExists = true;
              int i = 0;
              while (blnExists)
              {
                  // Path.Combine(경로, 파일명) = 경로+파일명
                  if (File.Exists(Path.Combine(dir, name)))
                  {
                      name = strName + ”(” + ++i + ”)” + strExt; // Test(3).txt
                  }
                  else
                  {
                      blnExists = false;
                  }
              }
              return name;
          }
          #endregion
      }
    }
    

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.