Form
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 namespace FileProperties 2 { 3 public partial class Form1 : Form 4 { 5 private string currentFolderPath; 6 7 public Form1() 8 { 9 InitializeComponent(); 10 } 11 12 protected void ClearAllFields() 13 { 14 listBoxFolders.Items.Clear(); 15 listBoxFiles.Items.Clear(); 16 textBoxFolder.Text = ""; 17 textBoxFolder.Text = ""; 18 textBoxFileName.Text = ""; 19 textBoxCreationTime.Text = ""; 20 textBoxLastAccessTime.Text = ""; 21 textBoxLastWriteTime.Text = ""; 22 textBoxFileSize.Text = ""; 23 } 24 25 protected void DisplayFileInfo(string fileFullName) 26 { 27 FileInfo theFile = new FileInfo(fileFullName); 28 if (!theFile.Exists) 29 throw new FileNotFoundException("File not found: " + fileFullName); 30 textBoxFileName.Text = theFile.Name; 31 textBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString(); 32 textBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString(); 33 textBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString(); 34 textBoxFileSize.Text = theFile.Length + " bytes"; 35 36 // enable move, copy, delete buttons 37 textBoxNewPath.Text = theFile.FullName; 38 textBoxNewPath.Enabled = true; 39 buttonCopyTo.Enabled = true; 40 buttonDelete.Enabled = true; 41 buttonMoveTo.Enabled = true; 42 } 43 44 protected void DisplayFolderList(string folderFullName) 45 { 46 DirectoryInfo theFolder = new DirectoryInfo(folderFullName); 47 if (!theFolder.Exists) 48 throw new DirectoryNotFoundException("Folder not found: " 49 + folderFullName); 50 ClearAllFields(); 51 DisableMoveFeatures(); 52 textBoxFolder.Text = theFolder.FullName; 53 currentFolderPath = theFolder.FullName; 54 55 // list all subfolders in folder 56 57 foreach (DirectoryInfo nextFolder in theFolder.GetDirectories()) 58 listBoxFolders.Items.Add(nextFolder.Name); 59 60 // list all files in folder 61 62 foreach (FileInfo nextFile in theFolder.GetFiles()) 63 listBoxFiles.Items.Add(nextFile.Name); 64 } 65 66 protected void OnDisplayButtonClick(object sender, EventArgs e) 67 { 68 try 69 { 70 string folderPath = textBoxInput.Text; 71 DirectoryInfo theFolder = new DirectoryInfo(folderPath); 72 if (theFolder.Exists) 73 { 74 DisplayFolderList(theFolder.FullName); 75 return; 76 } 77 FileInfo theFile = new FileInfo(folderPath); 78 if (theFile.Exists) 79 { 80 DisplayFolderList(theFile.Directory.FullName); 81 int index = listBoxFiles.Items.IndexOf(theFile.Name); 82 listBoxFiles.SetSelected(index, true); 83 return; 84 } 85 throw new FileNotFoundException("There is no file or folder with " 86 + "this name: " + textBoxInput.Text); 87 } 88 catch (Exception ex) 89 { 90 MessageBox.Show(ex.Message); 91 } 92 } 93 94 void DisableMoveFeatures() 95 { 96 textBoxNewPath.Text = ""; 97 textBoxNewPath.Enabled = false; 98 buttonCopyTo.Enabled = false; 99 buttonDelete.Enabled = false; 100 buttonMoveTo.Enabled = false; 101 } 102 103 protected void OnListBoxFilesSelected(object sender, EventArgs e) 104 { 105 try 106 { 107 string selectedString = listBoxFiles.SelectedItem.ToString(); 108 string fullFileName = Path.Combine(currentFolderPath, selectedString); 109 DisplayFileInfo(fullFileName); 110 } 111 catch (Exception ex) 112 { 113 MessageBox.Show(ex.Message); 114 } 115 } 116 117 protected void OnListBoxFoldersSelected(object sender, EventArgs e) 118 { 119 try 120 { 121 string selectedString = listBoxFolders.SelectedItem.ToString(); 122 string fullPathName = Path.Combine(currentFolderPath, selectedString); 123 DisplayFolderList(fullPathName); 124 } 125 catch (Exception ex) 126 { 127 MessageBox.Show(ex.Message); 128 } 129 } 130 131 protected void OnUpButtonClick(object sender, EventArgs e) 132 { 133 try 134 { 135 string folderPath = new FileInfo(currentFolderPath).DirectoryName; 136 DisplayFolderList(folderPath); 137 } 138 catch (Exception ex) 139 { 140 MessageBox.Show(ex.Message); 141 } 142 } 143 144 protected void OnDeleteButtonClick(object sender, EventArgs e) 145 { 146 try 147 { 148 string filePath = Path.Combine(currentFolderPath, 149 textBoxFileName.Text); 150 string query = "Really delete the file " + filePath + "?"; 151 if (MessageBox.Show(query, 152 "Delete File?", MessageBoxButtons.YesNo) == DialogResult.Yes) 153 { 154 File.Delete(filePath); 155 DisplayFolderList(currentFolderPath); 156 } 157 } 158 catch (Exception ex) 159 { 160 MessageBox.Show("Unable to delete file. The following exception" 161 + " occurred: " + ex.Message, "Failed"); 162 } 163 } 164 165 protected void OnMoveButtonClick(object sender, EventArgs e) 166 { 167 try 168 { 169 string filePath = Path.Combine(currentFolderPath, 170 textBoxFileName.Text); 171 string query = "Really move the file " + filePath + " to " 172 + textBoxNewPath.Text + "?"; 173 if (MessageBox.Show(query, 174 "Move File?", MessageBoxButtons.YesNo) == DialogResult.Yes) 175 { 176 File.Move(filePath, textBoxNewPath.Text); 177 DisplayFolderList(currentFolderPath); 178 } 179 } 180 catch (Exception ex) 181 { 182 MessageBox.Show("Unable to move file. The following exception" 183 + " occurred: " + ex.Message, "Failed"); 184 } 185 } 186 protected void OnCopyButtonClick(object sender, EventArgs e) 187 { 188 try 189 { 190 string filePath = Path.Combine(currentFolderPath, 191 textBoxFileName.Text); 192 string query = "Really copy the file " + filePath + " to " 193 + textBoxNewPath.Text + "?"; 194 if (MessageBox.Show(query, 195 "Copy File?", MessageBoxButtons.YesNo) == DialogResult.Yes) 196 { 197 File.Copy(filePath, textBoxNewPath.Text); 198 DisplayFolderList(currentFolderPath); 199 } 200 } 201 catch (Exception ex) 202 { 203 MessageBox.Show("Unable to copy file. The following exception" 204 + " occurred: " + ex.Message, "Failed"); 205 } 206 } 207 } 208 }
Designer
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 namespace FileProperties 2 { 3 4 partial class Form1 5 { 6 /// <summary> 7 /// Required designer variable. 8 /// </summary> 9 private System.ComponentModel.IContainer components = null; 10 11 /// <summary> 12 /// Clean up any resources being used. 13 /// </summary> 14 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 15 protected override void Dispose(bool disposing) 16 { 17 if (disposing && (components != null)) 18 { 19 components.Dispose(); 20 } 21 base.Dispose(disposing); 22 } 23 24 #region Windows Form Designer generated code 25 26 /// <summary> 27 /// Required method for Designer support - do not modify 28 /// the contents of this method with the code editor. 29 /// </summary> 30 private void InitializeComponent() 31 { 32 this.groupBox5 = new System.Windows.Forms.GroupBox(); 33 this.label14 = new System.Windows.Forms.Label(); 34 this.textBoxNewPath = new System.Windows.Forms.TextBox(); 35 this.buttonDelete = new System.Windows.Forms.Button(); 36 this.buttonCopyTo = new System.Windows.Forms.Button(); 37 this.buttonMoveTo = new System.Windows.Forms.Button(); 38 this.label3 = new System.Windows.Forms.Label(); 39 this.textBoxLastAccessTime = new System.Windows.Forms.TextBox(); 40 this.textBoxLastWriteTime = new System.Windows.Forms.TextBox(); 41 this.label6 = new System.Windows.Forms.Label(); 42 this.label1 = new System.Windows.Forms.Label(); 43 this.label5 = new System.Windows.Forms.Label(); 44 this.textBoxCreationTime = new System.Windows.Forms.TextBox(); 45 this.textBoxFileSize = new System.Windows.Forms.TextBox(); 46 this.label7 = new System.Windows.Forms.Label(); 47 this.label8 = new System.Windows.Forms.Label(); 48 this.label2 = new System.Windows.Forms.Label(); 49 this.label4 = new System.Windows.Forms.Label(); 50 this.buttonDisplay = new System.Windows.Forms.Button(); 51 this.textBoxFolder = new System.Windows.Forms.TextBox(); 52 this.buttonUp = new System.Windows.Forms.Button(); 53 this.textBoxInput = new System.Windows.Forms.TextBox(); 54 this.textBoxFileName = new System.Windows.Forms.TextBox(); 55 this.groupBox2 = new System.Windows.Forms.GroupBox(); 56 this.listBoxFiles = new System.Windows.Forms.ListBox(); 57 this.listBoxFolders = new System.Windows.Forms.ListBox(); 58 this.groupBox1 = new System.Windows.Forms.GroupBox(); 59 this.groupBox5.SuspendLayout(); 60 this.groupBox2.SuspendLayout(); 61 this.groupBox1.SuspendLayout(); 62 this.SuspendLayout(); 63 // 64 // groupBox5 65 // 66 this.groupBox5.Controls.Add(this.label14); 67 this.groupBox5.Controls.Add(this.textBoxNewPath); 68 this.groupBox5.Controls.Add(this.buttonDelete); 69 this.groupBox5.Controls.Add(this.buttonCopyTo); 70 this.groupBox5.Controls.Add(this.buttonMoveTo); 71 this.groupBox5.Location = new System.Drawing.Point(8, 368); 72 this.groupBox5.Name = "groupBox5"; 73 this.groupBox5.Size = new System.Drawing.Size(480, 88); 74 this.groupBox5.TabIndex = 13; 75 this.groupBox5.TabStop = false; 76 this.groupBox5.Text = "Move, Delete or Copy File"; 77 // 78 // label14 79 // 80 this.label14.Location = new System.Drawing.Point(8, 56); 81 this.label14.Name = "label14"; 82 this.label14.Size = new System.Drawing.Size(80, 16); 83 this.label14.TabIndex = 4; 84 this.label14.Text = "New Location"; 85 // 86 // textBoxNewPath 87 // 88 this.textBoxNewPath.Location = new System.Drawing.Point(88, 56); 89 this.textBoxNewPath.Name = "textBoxNewPath"; 90 this.textBoxNewPath.Size = new System.Drawing.Size(384, 20); 91 this.textBoxNewPath.TabIndex = 5; 92 // 93 // buttonDelete 94 // 95 this.buttonDelete.Location = new System.Drawing.Point(168, 16); 96 this.buttonDelete.Name = "buttonDelete"; 97 this.buttonDelete.Size = new System.Drawing.Size(75, 23); 98 this.buttonDelete.TabIndex = 2; 99 this.buttonDelete.Text = "Delete"; 100 this.buttonDelete.Click += new System.EventHandler(this.OnDeleteButtonClick); 101 // 102 // buttonCopyTo 103 // 104 this.buttonCopyTo.Location = new System.Drawing.Point(88, 16); 105 this.buttonCopyTo.Name = "buttonCopyTo"; 106 this.buttonCopyTo.Size = new System.Drawing.Size(75, 23); 107 this.buttonCopyTo.TabIndex = 1; 108 this.buttonCopyTo.Text = "Copy To"; 109 this.buttonCopyTo.Click += new System.EventHandler(this.OnCopyButtonClick); 110 // 111 // buttonMoveTo 112 // 113 this.buttonMoveTo.Location = new System.Drawing.Point(8, 16); 114 this.buttonMoveTo.Name = "buttonMoveTo"; 115 this.buttonMoveTo.Size = new System.Drawing.Size(75, 23); 116 this.buttonMoveTo.TabIndex = 0; 117 this.buttonMoveTo.Text = "Move To"; 118 this.buttonMoveTo.Click += new System.EventHandler(this.OnMoveButtonClick); 119 // 120 // label3 121 // 122 this.label3.Location = new System.Drawing.Point(240, 264); 123 this.label3.Name = "label3"; 124 this.label3.Size = new System.Drawing.Size(100, 16); 125 this.label3.TabIndex = 5; 126 this.label3.Text = "Creation time"; 127 // 128 // textBoxLastAccessTime 129 // 130 this.textBoxLastAccessTime.Enabled = false; 131 this.textBoxLastAccessTime.Location = new System.Drawing.Point(232, 112); 132 this.textBoxLastAccessTime.Name = "textBoxLastAccessTime"; 133 this.textBoxLastAccessTime.Size = new System.Drawing.Size(184, 20); 134 this.textBoxLastAccessTime.TabIndex = 1; 135 // 136 // textBoxLastWriteTime 137 // 138 this.textBoxLastWriteTime.Enabled = false; 139 this.textBoxLastWriteTime.Location = new System.Drawing.Point(16, 112); 140 this.textBoxLastWriteTime.Name = "textBoxLastWriteTime"; 141 this.textBoxLastWriteTime.Size = new System.Drawing.Size(184, 20); 142 this.textBoxLastWriteTime.TabIndex = 16; 143 // 144 // label6 145 // 146 this.label6.Location = new System.Drawing.Point(24, 312); 147 this.label6.Name = "label6"; 148 this.label6.Size = new System.Drawing.Size(120, 16); 149 this.label6.TabIndex = 8; 150 this.label6.Text = "Last modification time"; 151 // 152 // label1 153 // 154 this.label1.Location = new System.Drawing.Point(12, 9); 155 this.label1.Name = "label1"; 156 this.label1.Size = new System.Drawing.Size(280, 16); 157 this.label1.TabIndex = 32; 158 this.label1.Text = "Enter name of folder to be examined and click Display"; 159 // 160 // label5 161 // 162 this.label5.Location = new System.Drawing.Point(240, 312); 163 this.label5.Name = "label5"; 164 this.label5.Size = new System.Drawing.Size(100, 16); 165 this.label5.TabIndex = 7; 166 this.label5.Text = "Last access time"; 167 // 168 // textBoxCreationTime 169 // 170 this.textBoxCreationTime.Enabled = false; 171 this.textBoxCreationTime.Location = new System.Drawing.Point(232, 72); 172 this.textBoxCreationTime.Name = "textBoxCreationTime"; 173 this.textBoxCreationTime.Size = new System.Drawing.Size(184, 20); 174 this.textBoxCreationTime.TabIndex = 15; 175 // 176 // textBoxFileSize 177 // 178 this.textBoxFileSize.Enabled = false; 179 this.textBoxFileSize.Location = new System.Drawing.Point(24, 288); 180 this.textBoxFileSize.Name = "textBoxFileSize"; 181 this.textBoxFileSize.Size = new System.Drawing.Size(184, 20); 182 this.textBoxFileSize.TabIndex = 1; 183 // 184 // label7 185 // 186 this.label7.Location = new System.Drawing.Point(24, 264); 187 this.label7.Name = "label7"; 188 this.label7.Size = new System.Drawing.Size(100, 16); 189 this.label7.TabIndex = 10; 190 this.label7.Text = "File Size"; 191 // 192 // label8 193 // 194 this.label8.Location = new System.Drawing.Point(252, 105); 195 this.label8.Name = "label8"; 196 this.label8.Size = new System.Drawing.Size(100, 16); 197 this.label8.TabIndex = 29; 198 this.label8.Text = "Folders"; 199 // 200 // label2 201 // 202 this.label2.Location = new System.Drawing.Point(12, 105); 203 this.label2.Name = "label2"; 204 this.label2.Size = new System.Drawing.Size(136, 16); 205 this.label2.TabIndex = 33; 206 this.label2.Text = "Files"; 207 // 208 // label4 209 // 210 this.label4.Location = new System.Drawing.Point(16, 240); 211 this.label4.Name = "label4"; 212 this.label4.Size = new System.Drawing.Size(64, 16); 213 this.label4.TabIndex = 6; 214 this.label4.Text = "File name"; 215 // 216 // buttonDisplay 217 // 218 this.buttonDisplay.Location = new System.Drawing.Point(428, 25); 219 this.buttonDisplay.Name = "buttonDisplay"; 220 this.buttonDisplay.Size = new System.Drawing.Size(75, 23); 221 this.buttonDisplay.TabIndex = 35; 222 this.buttonDisplay.Text = "Display"; 223 this.buttonDisplay.Click += new System.EventHandler(this.OnDisplayButtonClick); 224 // 225 // textBoxFolder 226 // 227 this.textBoxFolder.Enabled = false; 228 this.textBoxFolder.Location = new System.Drawing.Point(8, 24); 229 this.textBoxFolder.Name = "textBoxFolder"; 230 this.textBoxFolder.Size = new System.Drawing.Size(384, 20); 231 this.textBoxFolder.TabIndex = 2; 232 // 233 // buttonUp 234 // 235 this.buttonUp.Location = new System.Drawing.Point(404, 73); 236 this.buttonUp.Name = "buttonUp"; 237 this.buttonUp.Size = new System.Drawing.Size(72, 24); 238 this.buttonUp.TabIndex = 34; 239 this.buttonUp.Text = "Up"; 240 this.buttonUp.Click += new System.EventHandler(this.OnUpButtonClick); 241 // 242 // textBoxInput 243 // 244 this.textBoxInput.Location = new System.Drawing.Point(12, 25); 245 this.textBoxInput.Name = "textBoxInput"; 246 this.textBoxInput.Size = new System.Drawing.Size(408, 20); 247 this.textBoxInput.TabIndex = 31; 248 // 249 // textBoxFileName 250 // 251 this.textBoxFileName.Enabled = false; 252 this.textBoxFileName.Location = new System.Drawing.Point(80, 240); 253 this.textBoxFileName.Name = "textBoxFileName"; 254 this.textBoxFileName.Size = new System.Drawing.Size(400, 20); 255 this.textBoxFileName.TabIndex = 1; 256 // 257 // groupBox2 258 // 259 this.groupBox2.Controls.Add(this.textBoxLastAccessTime); 260 this.groupBox2.Controls.Add(this.textBoxCreationTime); 261 this.groupBox2.Controls.Add(this.textBoxLastWriteTime); 262 this.groupBox2.Location = new System.Drawing.Point(8, 216); 263 this.groupBox2.Name = "groupBox2"; 264 this.groupBox2.Size = new System.Drawing.Size(480, 144); 265 this.groupBox2.TabIndex = 11; 266 this.groupBox2.TabStop = false; 267 this.groupBox2.Text = "Details of Selected File"; 268 // 269 // listBoxFiles 270 // 271 this.listBoxFiles.Location = new System.Drawing.Point(12, 121); 272 this.listBoxFiles.Name = "listBoxFiles"; 273 this.listBoxFiles.Size = new System.Drawing.Size(240, 134); 274 this.listBoxFiles.TabIndex = 28; 275 this.listBoxFiles.SelectedIndexChanged += new System.EventHandler(this.OnListBoxFilesSelected); 276 // 277 // listBoxFolders 278 // 279 this.listBoxFolders.Location = new System.Drawing.Point(260, 121); 280 this.listBoxFolders.Name = "listBoxFolders"; 281 this.listBoxFolders.Size = new System.Drawing.Size(240, 134); 282 this.listBoxFolders.TabIndex = 30; 283 this.listBoxFolders.SelectedIndexChanged += new System.EventHandler(this.OnListBoxFoldersSelected); 284 // 285 // groupBox1 286 // 287 this.groupBox1.Controls.Add(this.groupBox5); 288 this.groupBox1.Controls.Add(this.label6); 289 this.groupBox1.Controls.Add(this.label5); 290 this.groupBox1.Controls.Add(this.label3); 291 this.groupBox1.Controls.Add(this.textBoxFileSize); 292 this.groupBox1.Controls.Add(this.label7); 293 this.groupBox1.Controls.Add(this.textBoxFileName); 294 this.groupBox1.Controls.Add(this.label4); 295 this.groupBox1.Controls.Add(this.textBoxFolder); 296 this.groupBox1.Controls.Add(this.groupBox2); 297 this.groupBox1.Location = new System.Drawing.Point(4, 49); 298 this.groupBox1.Name = "groupBox1"; 299 this.groupBox1.Size = new System.Drawing.Size(496, 464); 300 this.groupBox1.TabIndex = 36; 301 this.groupBox1.TabStop = false; 302 this.groupBox1.Text = "Contents of folder"; 303 // 304 // Form1 305 // 306 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 307 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 308 this.ClientSize = new System.Drawing.Size(526, 525); 309 this.Controls.Add(this.label1); 310 this.Controls.Add(this.label8); 311 this.Controls.Add(this.label2); 312 this.Controls.Add(this.buttonDisplay); 313 this.Controls.Add(this.buttonUp); 314 this.Controls.Add(this.textBoxInput); 315 this.Controls.Add(this.listBoxFiles); 316 this.Controls.Add(this.listBoxFolders); 317 this.Controls.Add(this.groupBox1); 318 this.Name = "Form1"; 319 this.Text = "FilePropertiesAndMovement Sample"; 320 this.groupBox5.ResumeLayout(false); 321 this.groupBox5.PerformLayout(); 322 this.groupBox2.ResumeLayout(false); 323 this.groupBox2.PerformLayout(); 324 this.groupBox1.ResumeLayout(false); 325 this.groupBox1.PerformLayout(); 326 this.ResumeLayout(false); 327 this.PerformLayout(); 328 329 } 330 331 #endregion 332 333 private System.Windows.Forms.GroupBox groupBox5; 334 private System.Windows.Forms.Label label14; 335 private System.Windows.Forms.TextBox textBoxNewPath; 336 private System.Windows.Forms.Button buttonDelete; 337 private System.Windows.Forms.Button buttonCopyTo; 338 private System.Windows.Forms.Button buttonMoveTo; 339 private System.Windows.Forms.Label label3; 340 private System.Windows.Forms.TextBox textBoxLastAccessTime; 341 private System.Windows.Forms.TextBox textBoxLastWriteTime; 342 private System.Windows.Forms.Label label6; 343 private System.Windows.Forms.Label label1; 344 private System.Windows.Forms.Label label5; 345 private System.Windows.Forms.TextBox textBoxCreationTime; 346 private System.Windows.Forms.TextBox textBoxFileSize; 347 private System.Windows.Forms.Label label7; 348 private System.Windows.Forms.Label label8; 349 private System.Windows.Forms.Label label2; 350 private System.Windows.Forms.Label label4; 351 private System.Windows.Forms.Button buttonDisplay; 352 private System.Windows.Forms.TextBox textBoxFolder; 353 private System.Windows.Forms.Button buttonUp; 354 private System.Windows.Forms.TextBox textBoxInput; 355 private System.Windows.Forms.TextBox textBoxFileName; 356 private System.Windows.Forms.GroupBox groupBox2; 357 private System.Windows.Forms.ListBox listBoxFiles; 358 private System.Windows.Forms.ListBox listBoxFolders; 359 private System.Windows.Forms.GroupBox groupBox1; 360 361 362 363 364 } 365 }