存储整个文件结构的最简单方法是什么?

时间:2016-04-21 11:43:56

标签: c# directory

保留文件夹整个结构的最简单方法是什么,以便以后可以访问它来复制/删除?截至目前,以下是我正在做的事情,但是它没有在子目录中保留任何内容,多个不同的文件可以共享,因此删除整个子目录不是一个选项,至少不再复制根目录

      //get root directories located in mods folder

        List<string> rootDirectories = new List<string>();
        rootDirectories = Directory.GetDirectories("mods", "*").ToList();

        for (int i = 0; i < rootDirectories.Count(); i++)
        {
            Console.WriteLine("Root Directory: " + rootDirectories[i]);
        }

        //get subdirectories from each of those root directories

        List<List<string>> subDirectories = new List<List<string>>();

        for (int i = 0; i < rootDirectories.Count(); i++)
        {
            List<string> temp = new List<string>();
            temp = Directory.GetDirectories(rootDirectories[i], "*").ToList();

            //for (int j = 0; j < temp.Count(); j++)
            //{
            //    temp[j] = Path.GetFileName(temp[j]);
            //}

            subDirectories.Add(temp);
        }

        for (int i = 0; i < rootDirectories.Count(); i++)
        {
            for (int j = 0; j < subDirectories[i].Count(); j++)
            {
                Console.WriteLine("Sub Directory of " + rootDirectories[i] + ": " + subDirectories[i][j]);
            }
        }

        //get mod file(s) from each root folder

        List<List<string>> files = new List<List<string>>();

        for (int i = 0; i < rootDirectories.Count(); i++)
        {
            List<string> temp = new List<string>();
            temp = Directory.GetFiles(rootDirectories[i]).ToList();

            for (int k = 0; k < temp.Count(); k++)
            {
                temp[k] = Path.GetFileName(temp[k]);
            }

                files.Add(temp);
            for (int j = 0; j < temp.Count(); j++)
            {
                Console.WriteLine("File in " + rootDirectories[i] + ": " + temp[j]);
            }
        }

        //create new ModItem
        //add new ModItem to mods

        for (int i = 0; i < rootDirectories.Count(); i++)
        {
            try
            {
                mods.Add(new ModItem(rootDirectories[i], subDirectories[i], files[i]));
            }
            catch (Exception e)
            {

            }
        }

        for (int i = 0; i < mods.Count(); i++)
        {
            Console.WriteLine("Check to make sure the ModItem was added: " + mods[i].getRootDirectory());
        }

输出

Root Directory: mods\Blood Overhaul
Root Directory: mods\Cleaning
Root Directory: mods\SM Overhaul
Sub Directory of mods\Blood Overhaul: mods\Blood Overhaul\Meshes
Sub Directory of mods\Blood Overhaul: mods\Blood Overhaul\Textures
Sub Directory of mods\SM Overhaul: mods\SM Overhaul\Materials
Sub Directory of mods\SM Overhaul: mods\SM Overhaul\Meshes
Sub Directory of mods\SM Overhaul: mods\SM Overhaul\Textures
File in mods\Blood Overhaul: BloodOverhaul.file
File in mods\Cleaning: Cleaning.file
File in mods\SM Overhaul: SMOverhaul.file
Check to make sure the ModItem was added: mods\Blood Overhaul
Check to make sure the ModItem was added: mods\Cleaning
Check to make sure the ModItem was added: mods\SM Overhaul

如果我需要添加更多上下文,请告诉我,过去几天这一直困扰着我,我想让它解决。

1 个答案:

答案 0 :(得分:0)

您可以使用Recursive Method ::

public void CopyDirectory(DirectoryInfo dir)
{

// Copy Current directory (dir) file 
//Get inner directory list
//iterate directories and call CopyDirectory(dir); in iteration
CopyDirectory(dir);

}