Advertisement
Guest User

Untitled

a guest
May 14th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. namespace SoftUniFileLimitFixer
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.IO;
  6.     using System.IO.Compression;
  7.     using System.Linq;
  8.  
  9.     public static class FileLimitFixerMain
  10.     {
  11.         public static void Main()
  12.         {
  13.             string[] targetFolders = { "/bin", "/obj", "/packages", "/TestResults" };
  14.             string[] targetExtentions = { ".suo", ".StyleCop", ".zip", ".exe", ".Cache"};
  15.             try
  16.             {
  17.                 string currentDirectory = Directory.GetCurrentDirectory();
  18.                 string workingDirectory = currentDirectory + "/temp";
  19.                
  20.                 //create new dir
  21.                 foreach (string dirPath in Directory.GetDirectories(currentDirectory, "*",                      SearchOption.AllDirectories))
  22.                 {
  23.                     Directory.CreateDirectory(dirPath.Replace(currentDirectory, workingDirectory));
  24.                 }
  25.  
  26.                 //copy all files in the new dir
  27.                 foreach (string newPath in Directory.GetFiles(currentDirectory, "*.*", SearchOption.AllDirectories))
  28.                 {
  29.                     File.Copy(newPath, newPath.Replace(currentDirectory, workingDirectory), true);
  30.                 }
  31.  
  32.                 //delete all target folders
  33.                 foreach (string targetFolder in targetFolders)
  34.                 {
  35.                     string targetPath = workingDirectory + targetFolder;
  36.                     if (Directory.Exists(targetPath))
  37.                     {
  38.                         Directory.Delete(targetPath, true);
  39.                     }
  40.                 }
  41.  
  42.                 //delete all target files
  43.                 List<FileInfo> files = GetFiles(workingDirectory, targetExtentions);
  44.                 foreach (FileInfo file in files)
  45.                 {
  46.                     file.Attributes = FileAttributes.Normal;
  47.                     File.Delete(file.FullName);
  48.                 }
  49.  
  50.                 //extract the new folder to zip archive
  51.                 string zipPath = currentDirectory + "_Result.zip";
  52.                 ZipFile.CreateFromDirectory(workingDirectory, zipPath, CompressionLevel.Fastest, true);
  53.  
  54.                 //delete the temporary working folder
  55.                 Directory.Delete(workingDirectory, true);
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 Console.WriteLine(ex.Message);
  60.             }
  61.         }
  62.         private static List<FileInfo> GetFiles(string path, params string[] extensions)
  63.         {
  64.             List<FileInfo> list = new List<FileInfo>();
  65.             foreach (string ext in extensions)
  66.             {
  67.                 list.AddRange(new DirectoryInfo(path).GetFiles("*" + ext)
  68.                              .Where(p => p.Extension.Equals(ext, StringComparison.CurrentCultureIgnoreCase))
  69.                              .ToArray());
  70.             }
  71.             return list;
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement