Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# copy all files in directory and subdirectories

  /// <summary>
        /// Copy all files in Directory and Sub Directory
        /// </summary>
        /// <param name="fromDir">Source Dir</param>
        /// <param name="toDir">Target Dir</param>
        /// <param name="includeSubDir">Sub Directory</param>
        /// <param name="overWrite">Overwrite exsiting files/folder if true</param>
        public static void Copy(string fromDir, string toDir, bool includeSubDir, bool overWrite)
        {
            if (string.IsNullOrEmpty(fromDir))
            {
                throw new ArgumentException($"'{nameof(fromDir)}' cannot be null or empty", nameof(fromDir));
            }


            DirectoryInfo dir = new DirectoryInfo(fromDir);
            DirectoryInfo[] directories = dir.GetDirectories();

            if (!Directory.Exists(toDir))
            {
                CreateDirectory(toDir);
            }

            var files = dir.GetFiles();
            foreach (var file in files)
            {
                var tmpPath = Path.Combine(toDir, file.Name);
                file.CopyTo(tmpPath, overWrite);

            }

            if (includeSubDir)
            {
                foreach (var subDir in directories)
                {
                    var tmpPath = Path.Combine(toDir, subDir.Name);
                    Copy(subDir.FullName, tmpPath, includeSubDir, overWrite);
                }
            }
        
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: base c# 
Csharp :: how create two database conction in laravel 
Csharp :: check if multiple variables are null c# 
Csharp :: foreach for IEnumerable 
Csharp :: instantiate date time variable C# 
Csharp :: c# sbyte 
Csharp :: show datatable c# 
Csharp :: c# textbox kodu 
Csharp :: c# convert list to string and back 
Csharp :: convert json date to datetime c# 
Csharp :: set background from C# wpf 
Csharp :: c# yield return ienumerable 
Csharp :: instantiate object inside of object Unity 
Csharp :: c# how to initialize an array 
Csharp :: math in c# 
Csharp :: static keyword 
Csharp :: c# list initialize 
Csharp :: how to add object in dictionary in c# 
Csharp :: blazor use static json files 
Csharp :: Unity Input Key Message 
Csharp :: .net 3.1 bind json config 
Csharp :: txt.att.net not working 2021 
Csharp :: setxkbmap 
Csharp :: project camera view to texture 
Csharp :: close an open form when you open it again c# 
Csharp :: pause and resume thread C# 
Csharp :: mvc input number rounding 
Csharp :: unity update not called 
Csharp :: nodatime instant to datetime off set c# 
Csharp :: allelrt box wpf 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =