public static void DeleteFilesRecursive(string strRoot, string filter)
 {
     if (!String.IsNullOrEmpty(strRoot))
     {
         strRoot = FixPath(strRoot);
         if (Directory.Exists(strRoot))
         {
             foreach (string strFolder in Directory.EnumerateDirectoryPaths(strRoot))
             {
                 var directory = new DirectoryInfo(strFolder);
                 if ((directory.Attributes & FileAttributes.Hidden) == 0 && (directory.Attributes & FileAttributes.System) == 0)
                 {
                     DeleteFilesRecursive(strFolder, filter);
                 }
             }
             foreach (string strFile in Directory.EnumerateFilePaths(strRoot).Where(f => f.Contains(filter)))
             {
                 try
                 {
                     DeleteFile(strFile);
                 }
                 catch (Exception ex)
                 {
                     Logger.Error(ex);
                 }
             }
         }
     }
 }
Beispiel #2
0
        public static string DeleteFiles(Array arrPaths)
        {
            var strExceptions = string.Empty;

            for (var i = 0; i < arrPaths.Length; i++)
            {
                var strPath = (arrPaths.GetValue(i) ?? string.Empty).ToString();
                var pos     = strPath.IndexOf("'", StringComparison.Ordinal);
                if (pos != -1)
                {
                    // the (') represents a comment to the end of the line
                    strPath = strPath.Substring(0, pos);
                }

                strPath = FixPath(strPath).TrimStart('\\');
                if (!string.IsNullOrEmpty(strPath))
                {
                    strPath = Path.Combine(Globals.ApplicationMapPath, strPath);
                    if (strPath.EndsWith("\\") && Directory.Exists(strPath))
                    {
                        var directoryInfo   = new System.IO.DirectoryInfo(strPath);
                        var applicationPath = Globals.ApplicationMapPath + "\\";
                        if (directoryInfo.FullName.StartsWith(applicationPath, StringComparison.InvariantCultureIgnoreCase) &&
                            !directoryInfo.FullName.Equals(applicationPath, StringComparison.InvariantCultureIgnoreCase))
                        {
                            try
                            {
                                Globals.DeleteFolderRecursive(strPath);
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex);
                                strExceptions += $"Processing folder ({strPath}) Error: {ex.Message}{Environment.NewLine}";
                            }
                        }
                    }
                    else
                    {
                        if (File.Exists(strPath))
                        {
                            try
                            {
                                File.SetAttributes(strPath, FileAttributes.Normal);
                                File.Delete(strPath);
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex);
                                strExceptions += $"Processing file ({strPath}) Error: {ex.Message}{Environment.NewLine}";
                            }
                        }
                    }
                }
            }

            return(strExceptions);
        }
        public static void DeleteFolderRecursive(string strRoot)
        {
            strRoot = FixPath(strRoot);
            if (string.IsNullOrEmpty(strRoot) || !Directory.Exists(strRoot))
            {
                Logger.Info(strRoot + " does not exist. ");
                return;
            }

            foreach (string strFolder in Directory.EnumerateDirectoryPaths(strRoot))
            {
                DeleteFolderRecursive(strFolder);
            }

            foreach (string strFile in Directory.EnumerateFilePaths(strRoot))
            {
                try
                {
                    DeleteFile(strFile);
                }
                catch (Exception ex)
                {
                    Logger.Info(strRoot + " does not exist.");
                    Logger.Error(ex);
                }
            }

            try
            {
                Directory.SetAttributes(strRoot, FileAttributes.Normal);
                Directory.Delete(strRoot);
            }
            catch (Exception ex)
            {
                Logger.Info(strRoot + " does not exist.");
                Logger.Error(ex);
            }
        }
 public static void UnzipResources(ZipInputStream zipStream, string destPath)
 {
     try
     {
         var zipEntry = zipStream.GetNextEntry();
         while (zipEntry != null)
         {
             HtmlUtils.WriteKeepAlive();
             var localFileName = zipEntry.Name;
             var relativeDir   = Path.GetDirectoryName(zipEntry.Name);
             if (!string.IsNullOrEmpty(relativeDir) && (!Directory.Exists(Path.Combine(destPath, relativeDir))))
             {
                 Directory.Create(Path.Combine(destPath, relativeDir), true);
             }
             if (!zipEntry.IsDirectory && (!string.IsNullOrEmpty(localFileName)))
             {
                 var fileNamePath = FixPath(Path.Combine(destPath, localFileName));
                 try
                 {
                     if (File.Exists(fileNamePath))
                     {
                         File.SetAttributes(fileNamePath, FileAttributes.Normal);
                         File.Delete(fileNamePath);
                     }
                     FileStream objFileStream = null;
                     try
                     {
                         File.Create(fileNamePath);
                         objFileStream = File.Open(fileNamePath);
                         int intSize = 2048;
                         var arrData = new byte[2048];
                         intSize = zipStream.Read(arrData, 0, arrData.Length);
                         while (intSize > 0)
                         {
                             objFileStream.Write(arrData, 0, intSize);
                             intSize = zipStream.Read(arrData, 0, arrData.Length);
                         }
                     }
                     finally
                     {
                         if (objFileStream != null)
                         {
                             objFileStream.Close();
                             objFileStream.Dispose();
                         }
                     }
                 }
                 catch (Exception ex)
                 {
                     Logger.Error(ex);
                 }
             }
             zipEntry = zipStream.GetNextEntry();
         }
     }
     finally
     {
         if (zipStream != null)
         {
             zipStream.Close();
             zipStream.Dispose();
         }
     }
 }