2013-04-06 6 views
6

rar/zipファイルから抽出したファイルをフォルダに保存しています。
その後、そのフォルダ内のファイルをデータベースに保存しています。
問題は、サブディレクトリではないフォルダ内のファイルとそのファイルだけをに保存することです。C#のサブディレクトリを確認する

フォルダにサブディレクトリがあるかどうかを調べる方法。
私は

フォルダ内のファイルとディレクトリのリストを取得する方法
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)); 
} 

以下のコードを使用しています。データベースに保存しますか?ディレクトリを再帰的にトラバースする

答えて

2

あなたがDirectory.GetFilesを使用することができ、フォルダ内のファイルやディレクトリを取得するには()とDirectory.GetDirectories()

使用再帰またはキュー。再帰と

例:

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

    // Your code here 
} 
+0

ありがとうございます。これを試してみます。 + 1を与えるが、私は今15未満です:) – Jhonny

+0

+ 1について心配しないでください。お役に立てれば。 –

+0

あなたは正しいと受け入れるための評判は必要ありません。 –

2

この5月には役立ちます:

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

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

あなたはサブフォルダDirectoryInfoのを取得し、それらそれまではGetdirectories何も返さないを反復処理するGetDirectoriesを使用することができます。

関連する問題