C#创建子目录函数

Creating Subdirectory function in C#

我目前正在尝试创建一个函数代码,通过让用户输入目录路径然后在主要使用 Directory.GetDirectories(arwgs) 函数来获取路径的字符串数组。

此代码在第一次尝试时有效,但再次重新运行后,它会在我不想再次执行的相同目录中创建一个文件夹。

好:
创建的目录 S:\Shop....0\UnitCalFall

差:
创建的目录 S:\Shop0\UnitCalFall\UnitCalFall

创建的目录 S:\Shop0\UnitCalFall\UnitCalDone

我正在努力使该功能尽可能快速和集成,以防用户想要创建一个或两个以上的文件夹。

代码如下:

static void UnitCalFolderCheck(string[] sDirectoryPath, string[] NewFolder)
{
    //possible method can be constructed that checks for if a option UnitCallFolder has been created
    for (int index = 0; index < sDirectoryPath.Length; index++)
    {
        //for each directory in the path if a folder named as UnitCalDONE in order to store CSV files data that has already been stored and conducted. 
        //ig a foldered labeled as such is already created then do not create this folder
        string sCurrentPath = sDirectoryPath[index];

        //check if current directory path already is created by the newfolder length path 
        //NEED TO CREATE A VARIABLE THAT CHECKS IF ANY OF THE SUBSTRINGS ARE TRUE AND IF SO DO NOT CHECK FOR NEW DIRECTORY 
        bool bexist = false;

        //Console.WriteLine(sCurrentPath);
        //also check if a the current path also has the UnitCalFolder Done already. This is because the newDirpath
        //Will be a duplication of each folder and this can cause problems for the for loop
        //append for each dirpath the folder information
        for (int i = 0; i < NewFolder.Length; i++)
        {
            int  NewFolderLength = NewFolder[i].Length;

            string sNewDirPath = sCurrentPath + NewFolder[i];

            string substring = sCurrentPath.Substring(sCurrentPath.Length - NewFolderLength);

            //looping around the new possible created folders based on the substring paths
            foreach (string x in NewFolder)
            {
                //THIS DOESNT CHECK IF FOLDER IS DIFFERENT FROM THE OTHER CONTAINER
                // Console.WriteLine(x);
                if (!substring.Contains(x)) //not working propery 
                {
                    bexist = true;
                }
                else
                {
                    bexist = false;
                }
            }

            if (!Directory.Exists(sNewDirPath) && (bexist == true) )
            {
                Directory.CreateDirectory(sNewDirPath);
                Console.WriteLine("Directorys Created" + sNewDirPath);
            }
        }
    }
}

*一种粗略的修复方式,但回过头来看,这对后缀为“UnitCal”的文件夹也适用。至少对于我的目录。不是最优雅但有效。

static void UnitCalFolderCheck(string[] sDirectoryPath, string[] NewFolder)
        {
            
            
            //possible method can be constructed that checks for if a option UnitCallFolder has been created
            for (int index = 0; index < sDirectoryPath.Length; index++)
            {
                //for each directory in the path if a folder named as UnitCalDONE in order to store CSV files data that has already been stored and conducted. 
                //ig a foldered labeled as such is already created then do not create this folder
                string sCurrentPath = sDirectoryPath[index];
                //Console.WriteLine(sCurrentPath);
                //int[] iexist = new int[NewFolder.Count()]; //if substring exist already then dont create new directory 
                //int inotexist = 0;
                string sNewDirPath;
                string FolderIndexPath = "";
                //for every new folder in the path check if the directory with substring already exist
                for(int x = 0; x < NewFolder.Length; x++)
                {
                    int NewFolderLength = NewFolder.Length; //length of the new folder to append to newdirpath string
                    //string substring = sCurrentPath.Substring(sCurrentPath.Length - NewFolderLength);
                    //UnitCalDone folder first iteration
                    //UNitCalFall folder secnond iteration
                    Console.WriteLine("========================================================================");
                    Console.WriteLine(sCurrentPath);
                    Console.WriteLine(NewFolder[x]);
                    if (!sCurrentPath.Contains("UnitCal")) 
                    {
                        // iexist[x] = 0;
                        sNewDirPath = sCurrentPath + NewFolder[x];
                        // Determine whether the directory exists.
                        if (!Directory.Exists(sNewDirPath))
                        {
                            //check if new path already exist if so then do nothing 
                            Console.WriteLine("NEWPATH->>>");
                            //Directory.CreateDirectory(sNewDirPath);
                            Console.WriteLine(sNewDirPath); //check that none of this values contain the selected values 
                        }
                    }
                    else
                    {
                        //do nothing 
                        
                    }
                    
                }
            }//end of for loop method
        }//end of unitCalFolderCheckr code here*