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.
我将从rar / zip文件中提取的文件存储在一个文件夹中。之后,我将该文件夹中的文件存储到数据库中。问题是它只存储文件夹中的文件而不是子目录及其文件。
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中?
3 个解决方案
#1
2
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
}
#2
2
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不返回任何内容。
#3
1
You can use Directory.GetFileSystemEntries()
to get a list of both files and directories. http://msdn.microsoft.com/en-us/library/system.io.directory.getfilesystementries.aspx
您可以使用Directory.GetFileSystemEntries()来获取文件和目录的列表。 http://msdn.microsoft.com/en-us/library/system.io.directory.getfilesystementries.aspx
#1
2
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
}
#2
2
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不返回任何内容。
#3
1
You can use Directory.GetFileSystemEntries()
to get a list of both files and directories. http://msdn.microsoft.com/en-us/library/system.io.directory.getfilesystementries.aspx
您可以使用Directory.GetFileSystemEntries()来获取文件和目录的列表。 http://msdn.microsoft.com/en-us/library/system.io.directory.getfilesystementries.aspx