2016-03-07 44 views
-2

所以我爲lols創建了一些代碼,以便與我的學校服務器一起玩,並且我創建了一個文件夾生成器,它在每個az文件夾中創建文件夾az,並將文件夾放入5層文件夾中。當我運行該程序時,它是uubbberrr緩慢但它我想這個工作。有什麼辦法可以加快速度嗎?這是我的代碼如何加快生成循環的文件夾?

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Reflection; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Threading.Tasks; 

namespace Folder_Generator 
{ 
    class Program 
    { 
     private const int MF_BYCOMMAND = 0x00000000; 
     public const int SC_CLOSE = 0xF060; 

     [DllImport("user32.dll")] 
     public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags); 

     [DllImport("user32.dll")] 
     private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); 

     [DllImport("kernel32.dll", ExactSpelling = true)] 
     private static extern IntPtr GetConsoleWindow(); 

     static void Main(string[] args) 
     { 
      DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_CLOSE, MF_BYCOMMAND); 

      string topFolder = @"C:\"; 
      string subName = System.IO.Path.Combine(topFolder, "Files"); 
      System.IO.Directory.CreateDirectory(subName); 
      subName = @"C:\Files\"; 

      Console.ForegroundColor = ConsoleColor.Red; 
      Console.WriteLine("============================================"); 
      Console.ResetColor(); 
      Console.ForegroundColor = ConsoleColor.Blue; 
      Console.WriteLine("^    Folder Generator    ^"); 
      Console.WriteLine("^    by Pinga Muncher    ^"); 
      Console.ResetColor(); 
      Console.ForegroundColor = ConsoleColor.Red; 
      Console.WriteLine("^------------------------------------------^"); 
      Console.ResetColor(); 
      Console.ForegroundColor = ConsoleColor.Blue; 
      Console.WriteLine("^  Enter Yes to Start Generating  ^"); 
      Console.WriteLine("^   Enter No to Exit Program   ^"); 
      Console.ResetColor(); 
      Console.ForegroundColor = ConsoleColor.Red; 
      Console.WriteLine("============================================"); 
      Console.ResetColor(); 
      Console.ForegroundColor = ConsoleColor.Green; 

      Start: 
      string userInput = Console.ReadLine(); 
      switch (inputSwitch(userInput)) 
      { 
       case 0: 
        Console.WriteLine("Invalid Input, Enter Either (Yes/y) or (No/n)"); 
        goto Start; 
       case 1: 
        Console.WriteLine("Exiting Now"); 
        System.Threading.Thread.Sleep(1500); 
        System.Environment.Exit(-1); 
        break; 
       case 2: 
        for (int a = 1; a < 26; a++) 
        { 
         for (int b = 1; b < 26; b++) 
         { 
          for (int c = 1; c < 26; c++) 
          { 
           for (int d = 1; d < 26; d++) 
           { 
            string pathName1 = System.IO.Path.Combine(subName, switcheroo(a), switcheroo(b), switcheroo(c), switcheroo(d)); 
            System.IO.Directory.CreateDirectory(pathName1); 
            Extract("Folder_Generator", pathName1, "myFolder", "troll.png"); 
            Console.WriteLine("{0} - {1} - {2} - {3}", switcheroo(a), switcheroo(b), switcheroo(c), switcheroo(d)); 
           } 
           string pathName2 = System.IO.Path.Combine(subName, switcheroo(a), switcheroo(b), switcheroo(c)); 
           System.IO.Directory.CreateDirectory(pathName2); 
           Console.WriteLine("{0} - {1} - {2}", switcheroo(a), switcheroo(b), switcheroo(c)); 
          } 
          string pathName3 = System.IO.Path.Combine(subName, switcheroo(a), switcheroo(b)); 
          System.IO.Directory.CreateDirectory(pathName3); 
          Console.WriteLine("{0} - {1}", switcheroo(a), switcheroo(b)); 
         } 
         string pathName4 = System.IO.Path.Combine(subName, switcheroo(a)); 
         System.IO.Directory.CreateDirectory(pathName4); 
         Console.WriteLine(switcheroo(a)); 
        } 
        break; 
      } 
      Console.WriteLine("->----<- The Files Have Been Made :) ->----<-"); 
      Console.ReadKey(); 
     } 

     static int inputSwitch(string input) 
     { 
      string i = input; 
      switch (i) 
      { 
       case "Yes": 
        return 2; 
       case "yes": 
        return 2; 
       case "y": 
        return 2; 
       case "No": 
        return 1; 
       case "no": 
        return 1; 
       case "n": 
        return 1; 
      } 
      return 0; 
     } 

     static string switcheroo(int num) 
     { 
      int count = num; 
      switch (count) 
      { 
       case 1: 
        return "a"; 
       case 2: 
        return "b"; 
       case 3: 
        return "c"; 
       case 4: 
        return "d"; 
       case 5: 
        return "e"; 
       case 6: 
        return "f"; 
       case 7: 
        return "g"; 
       case 8: 
        return "h"; 
       case 9: 
        return "i"; 
       case 10: 
        return "j"; 
       case 11: 
        return "k"; 
       case 12: 
        return "l"; 
       case 13: 
        return "m"; 
       case 14: 
        return "n"; 
       case 15: 
        return "o"; 
       case 16: 
        return "p"; 
       case 17: 
        return "q"; 
       case 18: 
        return "r"; 
       case 19: 
        return "s"; 
       case 20: 
        return "t"; 
       case 21: 
        return "u"; 
       case 22: 
        return "v"; 
       case 23: 
        return "w"; 
       case 24: 
        return "x"; 
       case 25: 
        return "y"; 
       case 26: 
        return "z"; 
      } 
      return "a"; 
     } 

     public static void Extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName) 
     { 
      Assembly assembly = Assembly.GetCallingAssembly(); 

      using (Stream s = assembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName)) 
      using (BinaryReader r = new BinaryReader(s)) 
      using (FileStream fs = new FileStream(outDirectory + "\\" + resourceName, FileMode.OpenOrCreate)) 
      using (BinaryWriter w = new BinaryWriter(fs)) 
      { 
       w.Write(r.ReadBytes((int)s.Length)); 
      } 
     } 
    } 
} 
+2

這裏沒有多線程。您可以使用並行for循環,因爲一旦創建基礎,其餘不需要按特定順序發生。 – BugFinder

+0

您正在嘗試創建26^4或456.976文件夾(並且這只是最內部的循環)。難怪它很慢。 – Carra

+2

'啊我的眼睛...'。說真的,雖然你沒有多線程,這是一個主要瓶頸。此外,你只能進入4級深度(456,976個文件夾),5個深度將是11,881,376。你可以用遞歸來替換你的4個for循環,以提高可讀性。如果你擺脫了case語句並使用了一個數組,你會得到性能提升。最後,儘管你有計劃的惡作劇,但通過一次快速操作就可以刪除整個文件夾樹。 – Seph

回答

0

將此代碼添加到您的,然後調用start(subName)。 我使用CreateDirectory API以獲得更高的速度,並且我創建了並行工作的多任務。我希望它能幫助你。

[DllImport("kernel32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool CreateDirectory(string lpPathName, IntPtr lpSecurityAttributes); 

static string[] chrs = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; 

private static void start(string subName) 
{ 
    Parallel.For(0, chrs.Length, a => 
    { 
     createFolder(Path.Combine(subName, chrs[a])); 
    }); 
} 

private static void createFolder(string path) 
{ 
    CreateDirectory(path, IntPtr.Zero); 
    for (int b = 0; b < chrs.Length; b++) 
    { 
     string pathB = path + "\\" + chrs[b]; 
     CreateDirectory(pathB, IntPtr.Zero); 
     for (int c = 0; c < chrs.Length; c++) 
     { 
      string pathC = pathB + "\\" + chrs[c]; 
      CreateDirectory(pathC, IntPtr.Zero); 
      for (int d = 0; d < chrs.Length; d++) 
      { 
       string pathD = pathC + "\\" + chrs[d]; 
       CreateDirectory(pathD, IntPtr.Zero); 
       Extract("Folder_Generator", pathD, "myFolder", "troll.png"); 
      } 
     } 
     Console.WriteLine(path + "\\" + chrs[b]); 
    } 
}