■ サンプル
* フォルダの中身を確認するための簡易ツールなので出来はよくないですusing System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2 : Form { private const string DataRowNameOfExpectedValue = "Expected"; private const string DataRowNameOfRealValue = "Real"; private const string DataRowNameOfResultValue = "Result"; public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { List<string> results = this.GetDirectories(this.textBox1.Text); List<string> comparedValues = this.GetValuesFromFile(this.textBox2.Text); if (comparedValues != null) { DataTable resultTable = this.GetComparedResultDataTable(comparedValues, results); this.dataGridView1.DataSource = resultTable; } } #region ディレクトリをリストとして返す /// <summary> /// 指定したディレクトリ、その配下のファイルおよびサブディレクトリをリストとして返す。 /// </summary> /// <param name="folderPath">調査対象のディレクトリのパス</param> /// <remarks> /// [参考URL]http://blogs.yahoo.co.jp/dk521123/18011867.html /// </remarks> public List<string> GetDirectories(string folderPath) { List<string> returnValues = new List<string>(); if (Directory.Exists(folderPath)) { returnValues = this.GetDirectories(new System.IO.DirectoryInfo(folderPath), returnValues); returnValues.Sort(); } return returnValues; } public List<string> GetDirectories( System.IO.DirectoryInfo directoryInfo, List<string> returnValues) { // すべてのファイルをリストに追加する foreach (System.IO.FileInfo fileInfo in directoryInfo.GetFiles()) { // ファイルを追加する returnValues.Add(fileInfo.FullName); } // サブディレクトリ内を調べる (再帰) foreach (System.IO.DirectoryInfo subDirectoryInfo in directoryInfo.GetDirectories()) { this.GetDirectories(subDirectoryInfo, returnValues); } // ディレクトリを追加する returnValues.Add(directoryInfo.FullName); return returnValues; } #endregion #region 比較処理 private List<string> GetValuesFromFile(string fileName) { List<string> returnValues = null; try { string[] stringArray = System.IO.File.ReadAllLines(fileName); if (stringArray != null && stringArray.Count() > 0) { returnValues = stringArray.ToList(); returnValues.Sort(); } } catch (Exception) { returnValues = null; } return returnValues; } private DataTable GetComparedResultDataTable(List<string> expectedValues, List<string> realValues) { DataTable returnDataTable = new DataTable(); try { returnDataTable.Columns.Add(DataRowNameOfExpectedValue); returnDataTable.Columns.Add(DataRowNameOfRealValue); returnDataTable.Columns.Add(DataRowNameOfResultValue); foreach (var expectedValue in expectedValues) { DataRow dataRow = returnDataTable.NewRow(); dataRow[DataRowNameOfExpectedValue] = expectedValue; string resultToComare = realValues.FirstOrDefault(x => x.Equals(expectedValue)); if (resultToComare != null) { dataRow[DataRowNameOfRealValue] = resultToComare; dataRow[DataRowNameOfResultValue] = "OK"; } else { dataRow[DataRowNameOfRealValue] = String.Empty; dataRow[DataRowNameOfResultValue] = "NG"; } returnDataTable.Rows.Add(dataRow); } // [参考] http://qiita.com/kazuhirox/items/afa154232e636c9ca415 var exceptValues = realValues.Except<string>(expectedValues); foreach (var exceptValue in exceptValues.OrderBy(x => x)) { DataRow dataRow = returnDataTable.NewRow(); dataRow[DataRowNameOfExpectedValue] = String.Empty; dataRow[DataRowNameOfRealValue] = exceptValue; dataRow[DataRowNameOfResultValue] = "NG"; returnDataTable.Rows.Add(dataRow); } } catch (Exception) { } return returnDataTable; } #endregion } }
関連記事
ディレクトリの扱いについて
* 上記のサンプルの元になった記事https://blogs.yahoo.co.jp/dk521123/18011867.html