**編集:私のコードは実際に一種の仕事をしていることが指摘されています。ただし、「this.zip」ファイルはサブフォルダーとして識別されるため、「contents \ this.zip」であるフォルダーがリストされますが、これは私が望むものではありません。('contents \ newfolder \ this.zip'であるものだけを見つけようとしているので、'contents'ディレクトリの少なくとも1つのサブフォルダがあります)。.zipファイルだけでなく、実際のフォルダであるディレクトリのみを一覧表示するにはどうすればよいですか?
私は「this.zip」というタイトルのファイルを処理するプログラムを書いています。これらのファイルは通常、「contents」というフォルダーに直接配置されています。this.zipファイルが'contents'フォルダーに直接あるのか、それともサブフォルダーがあるのかを判断しようとしています。'contents'フォルダーが空かどうかを判断するために機能しているコードのセットがあり、サブディレクトリを含む'contents'フォルダーを見つけるように変更できると思いましたが、機能していません。
//This finds 'contents' folders that are empty
DirectoryInfo directory = new DirectoryInfo(txtbxOldFolder.Text);
DirectoryInfo[] folders = directory.GetDirectories("*contents*",SearchOption.AllDirectories);
Var query = from folder in folders
where folder.GetFileSystemInfos().Length == 0
select folder.FullName.ToString();
foreach (string str in query)
{
//this adds the path of any empty 'contents' folder
listEmptyFolder.Add(str);
}
クエリを変更した場合、whereステートメントの長さが1より大きい場合、サブディレクトリを持つものが見つかると思いましたが、機能していません。これは、私が試していることを達成するための最良の方法ですか、それともLINQを使用するこの試みは機能しますか?
//This is supposed to find 'contents' folders that have subdirectories, but it's unsuccessful
DirectoryInfo directory = new DirectoryInfo(txtbxOldFolder.Text);
DirectoryInfo[] folders = directory.GetDirectories("*contents*",SearchOption.AllDirectories);
Var query = from folder in folders
where folder.GetFileSystemInfos().Length > 1
select folder.FullName.ToString();
foreach (string str in query)
{
//this adds the path of any empty 'Contents' folder
listNestedFolders.Add(str);
}