.NET 폴 더 및 하위 파일 을 만 들 거나 삭제 하거나 복사 하 는 인 스 턴 스 방법


 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;


namespace WinFormsApp_OperateFileAndFolder

{

    public class OperateFileFolder

    {

        /// <summary>

        ///

        /// </summary>

        public string CreateFolder(string argPath)

        {

            string returnStr = "";

            try

            {

                if (System.IO.Directory.Exists(argPath))

                {

                    returnStr = " ";

 

                    return returnStr;

                }

                else

                {

                    System.IO.DirectoryInfo dirinfo = System.IO.Directory.CreateDirectory(argPath);

                    returnStr = " ! :" + System.IO.Directory.GetCreationTime(argPath);

                }

            }

            catch (Exception ee)

            {

                returnStr = " ! :" + ee.ToString();

            }

            return returnStr;

        }
        

        /// <summary>

        ///

        /// </summary>

        /// <param name="dir"></param>

        public void DeleteFolder(string dir)

        {

            // , ( )

            ////

            //if (dir[dir.Length - 1] != Path.DirectorySeparatorChar)

            //    dir += Path.DirectorySeparatorChar;

 

            if (Directory.Exists(dir)) //

            {

                foreach (string d in Directory.GetFileSystemEntries(dir))

                {

                    if (File.Exists(d))

                        File.Delete(d); //

                    else

                        DeleteFolder(d); //

                }

                Directory.Delete(dir); //

                Console.Write(dir + " ");

            }

            else

                Console.Write(dir + " "); //

        }

         
        /// <summary>

        /// copy

        /// 。

        /// </summary>

        public static void CopyDir(string srcPath, string aimPath)

        {

            try

            {

                //

                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)

                    aimPath += Path.DirectorySeparatorChar;

                //

                if (!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);

                // ,

                // copy

                // string[] fileList = Directory.GetFiles(srcPath);

                string[] fileList = Directory.GetFileSystemEntries(srcPath);

                //

                foreach (string file in fileList)

                {

                    // Copy

                    if (Directory.Exists(file))

                        CopyDir(file, aimPath + Path.GetFileName(file));

                    // Copy

                    else

                        File.Copy(file, aimPath + Path.GetFileName(file), true);

                }

            }

            catch (Exception e)

            {

                System.Windows.Forms.MessageBox.Show(e.ToString());

            }

        }


    }

}
 

좋은 웹페이지 즐겨찾기