Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何加快循环的文件夹生成?_C#_Multithreading_Performance_For Loop - Fatal编程技术网

C# 如何加快循环的文件夹生成?

C# 如何加快循环的文件夹生成?,c#,multithreading,performance,for-loop,C#,Multithreading,Performance,For Loop,因此,我为LOL创建了一些代码,让它们在我的学校服务器上玩得开心。我制作了一个文件夹生成器,在每个a-z文件夹中创建文件夹a-z,每个a-z文件夹包含5层文件夹。当我运行这个程序时,它的速度很慢,但我想它能完成任务。我有没有办法加快速度?这是我的密码 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Ru

因此,我为LOL创建了一些代码,让它们在我的学校服务器上玩得开心。我制作了一个文件夹生成器,在每个a-z文件夹中创建文件夹a-z,每个a-z文件夹包含5层文件夹。当我运行这个程序时,它的速度很慢,但我想它能完成任务。我有没有办法加快速度?这是我的密码

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));
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
运用系统反思;
使用System.Runtime.InteropServices;
使用系统文本;
使用System.Threading.Tasks;
命名空间文件夹\u生成器
{
班级计划
{
private const int MF_BYCOMMAND=0x00000000;
公共常数int SC_CLOSE=0xF060;
[DllImport(“user32.dll”)]
公共静态外部内部删除菜单(IntPtr HEMANU、int nPosition、int wFlags);
[DllImport(“user32.dll”)]
私有静态外部IntPtr GetSystemMenu(IntPtr hWnd,bool bRevert);
[DllImport(“kernel32.dll”,ExactSpelling=true)]
私有静态外部IntPtr GetConsoleWindow();
静态void Main(字符串[]参数)
{
DeleteMenu(GetSystemMenu(GetConsoleWindow(),false),SC_CLOSE,MF_BYCOMMAND);
字符串topFolder=@“C:\”;
string subName=System.IO.Path.Combine(topFolder,“Files”);
System.IO.Directory.CreateDirectory(子名称);
子名称=@“C:\Files\”;
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine(“================================================================”);
Console.ResetColor();
Console.ForegroundColor=ConsoleColor.Blue;
Console.WriteLine(“^Folder Generator^”);
Console.WriteLine(“^bypingamuncher^”);
Console.ResetColor();
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine(“^-------------------------------------------^”);
Console.ResetColor();
Console.ForegroundColor=ConsoleColor.Blue;
Console.WriteLine(^Enter Yes开始生成^;
Console.WriteLine(^Enter No退出程序^;
Console.ResetColor();
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine(“================================================================”);
Console.ResetColor();
Console.ForegroundColor=ConsoleColor.Green;
开始:
字符串userInput=Console.ReadLine();
开关(输入开关(用户输入))
{
案例0:
Console.WriteLine(“输入无效,请输入(是)或(否)”);
转到开始;
案例1:
Console.WriteLine(“立即退出”);
系统.线程.线程.睡眠(1500);
系统。环境。退出(-1);
打破
案例2:
对于(int a=1;a<26;a++)
{
对于(intb=1;b<26;b++)
{
对于(int c=1;c<26;c++)
{
对于(int d=1;d<26;d++)
{
string pathName1=System.IO.Path.Combine(子名称、switcheroo(a)、switcheroo(b)、switcheroo(c)、switcheroo(d));
System.IO.Directory.CreateDirectory(路径名1);
提取(“文件夹生成器”,路径名1,“myFolder”,“troll.png”);
Console.WriteLine(“{0}-{1}-{2}-{3}”),switchero(a),switchero(b),switchero(c),switchero(d));
}
stringpathname2=System.IO.Path.Combine(子名称、switcheroo(a)、switcheroo(b)、switcheroo(c));
System.IO.Directory.CreateDirectory(路径名2);
Console.WriteLine(“{0}-{1}-{2}”、switchero(a)、switchero(b)、switchero(c));
}
stringpathname3=System.IO.Path.Combine(子名称、switcheroo(a)、switcheroo(b));
System.IO.Directory.CreateDirectory(路径名3);
WriteLine(“{0}-{1}”,switchero(a),switchero(b));
}
字符串pathName4=System.IO.Path.Combine(子名称,switcheroo(a));
System.IO.Directory.CreateDirectory(路径名4);
控制台写入线(switcheroo(a));
}
打破
}

Console.WriteLine(“->-->将此代码添加到您的代码中,然后调用start(子名称)。 我使用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]);
    }
}
[DllImport(“kernel32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
静态extern bool CreateDirectory(字符串lpPathName、IntPtr lpSecurityAttributes);
静态字符串[]chrs=新字符串[]{“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”};
私有静态无效开始(字符串子名称)
{
Parallel.For(0,chrs.Length,a=>
{
createFolder(Path.Combine(子名称,chrs[a]);
});
}
私有静态void createFolder(字符串路径)
{
CreateDirectory(路径,IntPtr.Zero);
for(int b=0;b