Example #1
0
 internal static void DeleteDirOrFile(string destPath)
 {
     if (!FileStatusHelper.IsFileOccupied(destPath))
     {
         FileInfo flinfo = null;
         try
         {
             while (Directory.Exists(destPath))
             {
                 Directory.Delete(destPath, true);
             }
             flinfo = new FileInfo(destPath);
             while (flinfo.Exists)
             {
                 flinfo.Delete();
                 flinfo.Refresh();
                 Thread.Sleep(1000);
             }
         }
         catch (Exception ex)
         {
             throw ex;
         }
         finally
         {
             while (flinfo.Exists)
             {
                 flinfo.Delete();
                 flinfo.Refresh();
                 Thread.Sleep(1000);
             }
             flinfo = null;
         }
     }
 }
Example #2
0
        public static bool CopyDirectory(string sourcePath, string destPath, bool overwriteexisting = false)
        {
            bool ret = false;

            try
            {
                sourcePath = sourcePath.EndsWith(@"\") ? sourcePath : sourcePath + @"\";
                destPath   = destPath.EndsWith(@"\") ? destPath : destPath + @"\";

                if (Directory.Exists(sourcePath))
                {
                    if (Directory.Exists(destPath) == false)
                    {
                        Directory.CreateDirectory(destPath);
                    }

                    string destFilePath;
                    foreach (string fls in Directory.GetFiles(sourcePath))
                    {
                        if (!FileStatusHelper.IsFileOccupied(fls))
                        {
                            FileInfo flinfo = new FileInfo(fls);
                            destFilePath = destPath + "\\" + flinfo.Name;
                            flinfo.CopyTo(destFilePath, overwriteexisting);
                            //else
                            //{
                            //    if (flinfo.LastWriteTime.CompareTo(new FileInfo(destFilePath).LastWriteTime) > 0)
                            //    {
                            //        flinfo.CopyTo(destFilePath, true);
                            //    }
                            //}
                        }
                    }
                    foreach (string drs in Directory.GetDirectories(sourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, destPath + drinfo.Name, overwriteexisting) == false)
                        {
                            ret = false;
                        }
                    }
                }
                ret = true;
            }
            catch (Exception ex)
            {
                ret = false;
                throw ex;
            }
            return(ret);
        }