获取文件夹所有及其所有目录的修改时间
时间:2015/11/15
背景
- 之前用过坚果云做同步时,发现它会监视文件夹的修改工作,进而进行相应的同步;最近同学搭建服务器时,会涉及到文件的上传等工作,想做一个基于文件监督的日志生成工具,因此做了这样一个初步的文件夹修改日期查看的东西
界面
主要有选择文件夹按钮、获取文件夹及其修改信息按钮,以及用于显示的文本框
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace FileChangeMonitor
{/// /// 检测文件夹及其子目录中所有文件的创建时间/// public partial class Form1 : Form{private string dirPath = @"C:\Users\Administrator\Desktop\TestUp";private string txtForShow;public Form1(){InitializeComponent();tbDir.Text = dirPath;}private void btGetInfo_Click(object sender, EventArgs e){txtForShow = "";if ((new DirectoryInfo(dirPath)).Exists)RecursionForDir(dirPath, 0);elseMessageBox.Show("文件夹不存在!请重新选择");}/// /// 对文件夹进行递归/// /// /// private void RecursionForDir(string dirPath, int tabNum ){DirectoryInfo dirInfo = new DirectoryInfo(dirPath);foreach (FileInfo fi in dirInfo.GetFiles()){ShowFileInfo(fi.Name + "--" + fi.LastWriteTime.ToString("yy/MM/dd/hh:mm:ss"), tabNum);}tabNum++;foreach (DirectoryInfo fi in dirInfo.GetDirectories()){ShowDirInfo(">-"+fi.Name + "--" + fi.LastWriteTime.ToString("yy/MM/dd/hh:mm:ss"), tabNum-1);RecursionForDir(fi.FullName, tabNum);}tbInfo.Text = txtForShow;}/// /// 输出文件名称/// /// /// private void ShowFileInfo(string fileName, int tabNum){for (int i = 0; i /// 输出文件夹名称/// /// /// private void ShowDirInfo(string dirName, int tabNum){for (int i = 0; i /// 选择文件夹/// /// /// private void btnChooseDir_Click(object sender, EventArgs e){if (dirChooseDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK){dirPath = dirChooseDlg.SelectedPath;}}}
}