简体   繁体   English

检查C#中的子目录

[英]Check for Sub directory in C#

I am storing the files extracted from a rar/zip file in a folder. 我将从rar / zip文件中提取的文件存储在一个文件夹中。
After that I am storing the files inside that folder to a database. 之后,我将该文件夹中的文件存储到数据库中。
The problem is it stores only the files in the folder not the sub directories and its files . 问题是它只存储文件夹中的文件而不存储子目录及其文件

How to find if the folder has any sub directories. 如何查找文件夹是否有任何子目录。
I am using the code below 我正在使用下面的代码

 Directory.CreateDirectory(StorageRoot + path + New_folder);
 decom(StorageRoot + path + New_folder, StorageRoot + path + file.FileName);
 foreach (var access_file in Directory.GetFiles(StorageRoot + path + New_folder))
 {                     
     string new_name=System.IO.Path.GetFileName(access_file);
     FileInfo f = new FileInfo(StorageRoot + path + New_folder+ "/" + new_name);
     int new_size = unchecked((int)f.Length);
     string new_path = path + New_folder;
     statuses.Add(new FilesStatus(new_name, new_size, new_path, StorageRoot, data));
 }

How to get the list of files and directories in a folder. 如何获取文件夹中的文件和目录列表。 and save them in db? 并将它们保存在db中?

To get the files and directories in a folder you can use Directory.GetFiles() and Directory.GetDirectories() 要获取文件夹中的文件和目录,可以使用Directory.GetFiles()和Directory.GetDirectories()

Use recursion or Queue to recursively traverse directories. 使用递归或队列递归遍历目录。

Example with recursion: 递归示例:

void Traverse(string directory)
{
    foreach(var dir in Directories.GetDirectories(directory))
    {
         Traverse(directory);
    }

    // Your code here
}

This may helps: 这可能有助于:

    System.IO.DirectoryInfo info = new System.IO.DirectoryInfo("YOUR PATH");

    //List of directories
    var result = info.GetDirectories().Select(i => i.FullName);

You can use GetDirectories to get sub folder DirectoryInfo's and iterate through them untill Getdirectories returns nothing. 您可以使用GetDirectories获取子文件夹DirectoryInfo并迭代它们直到Getdirectories返回任何内容。

You can use Directory.GetFileSystemEntries() to get a list of both files and directories. 您可以使用Directory.GetFileSystemEntries()来获取文件和目录的列表。 http://msdn.microsoft.com/en-us/library/system.io.directory.getfilesystementries.aspx http://msdn.microsoft.com/en-us/library/system.io.directory.getfilesystementries.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM