修订版 | 14 (tree) |
---|---|
时间 | 2012-04-04 16:42:52 |
作者 | daydream_model3 |
ボリュームシリアルを取得するために、ManagementObjectクラスへの参照を追加。
生成されるリストファイルのヘッダー、フッター出力処理を追加。
@@ -9,28 +9,72 @@ | ||
9 | 9 | class WinTreeCreator |
10 | 10 | { |
11 | 11 | private WinTreeSettings settings = new WinTreeSettings(); |
12 | - int directoryCount; | |
13 | - int fileCount; | |
12 | + int totalDirectoryCount = 0; | |
13 | + int totalFileCount = 0; | |
14 | + long totalFileSize = 0; | |
14 | 15 | |
15 | 16 | public WinTreeCreator(DirectoryInfo targetDirectory, FileInfo saveFile) |
16 | 17 | { |
17 | - directoryCount = 0; | |
18 | - fileCount = 0; | |
19 | - | |
20 | - using (TextWriter tw = saveFile.CreateText()) | |
18 | + try | |
21 | 19 | { |
22 | - ExportFileHeader(targetDirectory); | |
23 | - CreateDirectoryLists(targetDirectory, 0, tw); | |
24 | - ExportFileFooter(); | |
20 | + using (TextWriter tw = saveFile.CreateText()) | |
21 | + { | |
22 | + ExportFileHeader(targetDirectory, tw); | |
23 | + CreateDirectoryLists(targetDirectory, 0, tw); | |
24 | + ExportFileFooter(targetDirectory, tw); | |
25 | + } | |
25 | 26 | } |
27 | + catch (Exception err) | |
28 | + { | |
29 | + System.Windows.Forms.MessageBox.Show( | |
30 | + err.Message, "wintree7", | |
31 | + System.Windows.Forms.MessageBoxButtons.OK, | |
32 | + System.Windows.Forms.MessageBoxIcon.Error); | |
33 | + } | |
26 | 34 | } |
27 | 35 | |
28 | - private void ExportFileHeader(DirectoryInfo targetDirectory) | |
36 | + private void ExportFileHeader(DirectoryInfo targetDirectory, TextWriter writer) | |
29 | 37 | { |
38 | + writer.WriteLine( | |
39 | + "WinTree7 Ver " | |
40 | + + System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion | |
41 | + + " " /// @todo 桁数に合うようにスペースを追加する | |
42 | + + DateTime.Now.ToString()); | |
43 | + | |
44 | + DriveInfo targetDrive = new DriveInfo(targetDirectory.Root.Name); | |
45 | + System.Management.ManagementObject mo = new System.Management.ManagementObject("Win32_LogicalDisk=\"" + targetDrive.Name.Trim('\\') + "\""); | |
46 | + | |
47 | + writer.WriteLine( | |
48 | + "ドライブ " + targetDrive.Name | |
49 | + + " のボリュームラベルは" + (targetDrive.VolumeLabel == "" ? "ありません。" : " " + targetDrive.VolumeLabel) | |
50 | + + " (" + mo.Properties["VolumeSerialNumber"].Value.ToString().Substring(0, 4) | |
51 | + + "-" + mo.Properties["VolumeSerialNumber"].Value.ToString().Substring(4, 4) + ")" | |
52 | + ); | |
53 | + writer.WriteLine( | |
54 | + "フォルダは " | |
55 | + + targetDirectory.FullName | |
56 | + ); | |
57 | + writer.WriteLine(); //空行 | |
58 | + writer.WriteLine( | |
59 | + "ファイル/フォルダ名" | |
60 | + + " " /// @todo 桁数に合うようにスペースを追加する | |
61 | + + "バイト数 属性 更新日付 更新時刻" | |
62 | + ); | |
63 | + writer.WriteLine(new string(' ', settings.Style.colomnLimit / 2).Replace(" ", settings.Style.fullWidth.rule)); | |
30 | 64 | } |
31 | 65 | |
32 | - private void ExportFileFooter() | |
66 | + private void ExportFileFooter(DirectoryInfo targetDirectory, TextWriter writer) | |
33 | 67 | { |
68 | + writer.WriteLine(new string(' ', settings.Style.colomnLimit / 2).Replace(" ", settings.Style.fullWidth.rule)); | |
69 | + writer.WriteLine("{0} 個のフォルダと {1} 個 {2:N0} Byte のファイルがあります。", totalDirectoryCount, totalFileCount, totalFileSize); | |
70 | + | |
71 | + DriveInfo targetDrive = new DriveInfo(targetDirectory.Root.Name); | |
72 | + float remainSpacePercent = (float)(targetDrive.TotalFreeSpace) / (float)(targetDrive.TotalSize); | |
73 | + System.Globalization.NumberFormatInfo nfmt = new System.Globalization.NumberFormatInfo(); | |
74 | + nfmt.PercentDecimalDigits = (remainSpacePercent >= 0.10 ? 1 : 2); | |
75 | + nfmt.PercentSymbol = "%"; | |
76 | + | |
77 | + writer.WriteLine("ディスク空き容量は {0:N0} Byte ({1}) あります。", targetDrive.TotalFreeSpace, remainSpacePercent.ToString("P", nfmt)); | |
34 | 78 | } |
35 | 79 | |
36 | 80 | private void CreateDirectoryLists(DirectoryInfo currentDirectory, int depth, TextWriter writer) |
@@ -42,17 +86,20 @@ | ||
42 | 86 | fmt += " "; |
43 | 87 | } |
44 | 88 | writer.WriteLine(fmt + "{0}", currentDirectory.Name); |
45 | - | |
46 | - DirectoryInfo[] subDirectory = currentDirectory.GetDirectories(); | |
47 | - Array.Sort<DirectoryInfo>(subDirectory, (a, b) => a.Name.CompareTo(b.Name)); //FAT32対策にソート | |
48 | 89 | |
49 | - foreach(DirectoryInfo sub in subDirectory){ | |
50 | - CreateDirectoryLists(sub, depth + 1, writer); | |
90 | + if (settings.Other.searchRecursive) | |
91 | + { | |
92 | + DirectoryInfo[] subDirectory = currentDirectory.GetDirectories(); | |
93 | + Array.Sort<DirectoryInfo>(subDirectory, (a, b) => a.Name.CompareTo(b.Name)); //FAT32対策にソート | |
94 | + | |
95 | + foreach (DirectoryInfo sub in subDirectory) | |
96 | + { | |
97 | + CreateDirectoryLists(sub, depth + 1, writer); | |
98 | + } | |
99 | + totalDirectoryCount += subDirectory.Length; | |
51 | 100 | } |
52 | 101 | |
53 | 102 | CreateFileList(currentDirectory, depth + 1, writer); |
54 | - | |
55 | - directoryCount += subDirectory.Length; | |
56 | 103 | } |
57 | 104 | |
58 | 105 | private void CreateFileList(DirectoryInfo currentDirectory, int depth, TextWriter writer) |
@@ -96,9 +143,10 @@ | ||
96 | 143 | foreach (FileInfo fi in files) |
97 | 144 | { |
98 | 145 | writer.WriteLine(fmt + "{0}", fi.Name); |
146 | + totalFileSize += fi.Length; | |
99 | 147 | } |
100 | 148 | |
101 | - fileCount += files.Length; | |
149 | + totalFileCount += files.Length; | |
102 | 150 | } |
103 | 151 | } |
104 | 152 | } |