6

I am storing the files extracted from a rar/zip file in a folder.
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?

4

3 回答 3

2

要获取文件夹中的文件和目录,您可以使用 Directory.GetFiles() 和 Directory.GetDirectories()

使用递归或队列递归遍历目录。

递归示例:

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

    // Your code here
}
于 2013-04-06T11:17:27.507 回答
2

这可能会有所帮助:

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

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

您可以使用GetDirectories获取子文件夹 DirectoryInfo 并遍历它们,直到Getdirectories什么都不返回。

于 2013-04-06T11:36:27.177 回答
1

您可以使用Directory.GetFileSystemEntries()来获取文件和目录的列表。 http://msdn.microsoft.com/en-us/library/system.io.directory.getfilesystementries.aspx

于 2013-04-06T13:34:02.960 回答