#region 文件夹操作
/// <summary>
/// 选择路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenDir_Click(object sender, EventArgs e)
{
// 从工具箱拖入一个FolderBrowserDialog,命名为fbd。除了拖入,还可直接定义
FolderBrowserDialog fbd = new FolderBrowserDialog();
// 设置文件夹选择框提示文本
fbd.Description = "请选择一个文件夹:";
// 设置默认位置为桌面
fbd.RootFolder = Environment.SpecialFolder.DesktopDirectory;
// 设置是否显示“新建文件夹”按钮
fbd.ShowNewFolderButton = false;
// 设置默认选中的文件夹为本地目录
fbd.SelectedPath = @"e:";
// 设置默认选中的文件夹为网络路径
//this.fbd.SelectedPath = @"\192.168.1.1";
// 显示对话框,并返回已选中的文件夹
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.tbDir.Text = fbd.SelectedPath;
}
}
/// <summary>
/// 文件夹属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetDirInfo_Click(object sender, EventArgs e)
{
string dirName = this.tbDir.Text;
if (string.IsNullOrEmpty(dirName))
{
this.tbInfo.AppendText("文件夹不能空
");
return;
}
// 检查文件夹是否存在
if (Directory.Exists(dirName))
{
// 根据路径获得文件夹属性
DirectoryInfo info = new DirectoryInfo(dirName);
this.tbInfo.AppendText(string.Format("完整路径:{0}
", info.FullName));
this.tbInfo.AppendText(string.Format("获取目录的根:{0}
", info.Root));
this.tbInfo.AppendText(string.Format("获取目录的父目录:{0}
", info.Parent));
this.tbInfo.AppendText(string.Format("创建时间:{0}
", info.CreationTime));
this.tbInfo.AppendText(string.Format("最后一次访问时间:{0}
", info.LastAccessTime));
this.tbInfo.AppendText(string.Format("最后一次写入时间:{0}
", info.LastWriteTime));
} else
{
this.tbInfo.AppendText(string.Format("文件夹不存在{0}
", dirName));
}
}
/// <summary>
/// 文件夹权限
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetDirSec_Click(object sender, EventArgs e)
{
//取得文件夹的访问权限
string dirName = this.tbDir.Text;
if (string.IsNullOrEmpty(dirName))
{
this.tbInfo.AppendText("文件夹不能空
");
return;
}
DirectoryInfo dirInfo = new DirectoryInfo(dirName);
// 需引用命名空间System.Security.AccessControl;
// 取得访问控制列表ACL信息
DirectorySecurity sec = dirInfo.GetAccessControl(AccessControlSections.Access);
foreach (FileSystemAccessRule rule in
sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
{
// 文件夹名称
tbInfo.AppendText(dirName + " ");
// 取得Windows账号或sid
tbInfo.AppendText(rule.IdentityReference.Value + " ");
// 取得文件夹权限
tbInfo.AppendText(rule.FileSystemRights.ToString() + "
");
}
}
/// <summary>
/// 遍历子文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetDirFiles_Click(object sender, EventArgs e)
{
string dirName = this.tbDir.Text;
if (string.IsNullOrEmpty(dirName))
{
this.tbInfo.AppendText("文件夹不能空
");
return;
}
//遍历文件夹中的文件
DirectoryInfo info = new DirectoryInfo(dirName);
foreach (FileInfo fInfo in info.GetFiles())
{
this.tbInfo.AppendText(fInfo.FullName + "
");
}
}
/// <summary>
/// 遍历子文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetSubDir_Click(object sender, EventArgs e)
{
string dirName = this.tbDir.Text;
if (string.IsNullOrEmpty(dirName))
{
this.tbInfo.AppendText("文件夹不能空
");
return;
}
//遍历文件夹中的子文件夹
DirectoryInfo info = new DirectoryInfo(dirName);
foreach (DirectoryInfo dInfo in info.GetDirectories())
{
this.tbInfo.AppendText(dInfo.FullName + "
");
}
}
/// <summary>
/// 遍历全部子文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetAllSubDir_Click(object sender, EventArgs e)
{
string dirName = this.tbDir.Text;
if (string.IsNullOrEmpty(dirName))
{
this.tbInfo.AppendText("文件夹不能空
");
return;
}
//使用递归方法遍历文件夹中所有的子文件夹
DirectoryInfo info = new DirectoryInfo(dirName);
foreach (DirectoryInfo dInfo in info.GetDirectories())
{
//ReadDirs(dInfo.FullName);
ReadDirs(dInfo.FullName,0);
this.tbInfo.AppendText(dInfo.FullName + "
");
}
}
private void ReadDirs(string dirName)
{
// 递归读取子文件夹
DirectoryInfo info = new DirectoryInfo(dirName);
foreach (DirectoryInfo dInfo in info.GetDirectories())
{
ReadDirs(dInfo.FullName);
this.tbInfo.AppendText(dInfo.FullName + "
");
}
}
private void ReadDirs(string dirName, int level)
{
// 记录文件夹读取深度
level++;
// 当遍历深度小于设定值时才继续读取
if (level < totalLevel)
{
DirectoryInfo info = new DirectoryInfo(dirName);
#region 显示文件信息
foreach (FileInfo fInfo in info.GetFiles())
{
this.tbInfo.AppendText(fInfo.FullName + "
");
}
#endregion
#region 显示子文件夹
foreach (DirectoryInfo dInfo in info.GetDirectories())
{
ReadDirs(dInfo.FullName, level);
this.tbInfo.AppendText(dInfo.FullName + "
");
}
#endregion
}
}
/// <summary>
/// 删除文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeleteDir_Click(object sender, EventArgs e)
{
string dirName = this.tbDir.Text;
if (string.IsNullOrEmpty(dirName))
{
this.tbInfo.AppendText("文件夹不能空
");
return;
}
if (Directory.Exists(dirName))
{
if(MessageBox.Show("您确定要删除指定文件夹吗?","确认框",
MessageBoxButtons.YesNo,MessageBoxIcon.Question)
== System.Windows.Forms.DialogResult.Yes)
{
Directory.Delete(dirName);
}
}
}
/// <summary>
/// 移动文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnMoveDir_Click(object sender, EventArgs e)
{
string dirName = this.tbDir.Text;
if (string.IsNullOrEmpty(dirName))
{
this.tbInfo.AppendText("文件夹不能空
");
return;
}
if (Directory.Exists(dirName))
{
if (MessageBox.Show("您确定要移动文件夹吗?", "确认框",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== System.Windows.Forms.DialogResult.Yes)
{
//源路径和目标路径必须具有相同的根。移动操作在卷之间无效
Directory.Move(dirName, @"C:UsersDaiDesktop截图222");
}
}
}
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCreateDir_Click(object sender, EventArgs e)
{
// appPath = System.Windows.Forms.Application.StartupPath + "\";
string dirName = appPath + DateTime.Now.ToString("yyyyMMddHHmmss");
Directory.CreateDirectory(dirName);
this.tbInfo.AppendText(string.Format("当前工作目录:{0}
", Directory.GetCurrentDirectory()));
Directory.SetCurrentDirectory(@"c:");
Directory.CreateDirectory(DateTime.Now.ToString("yyyyMMddHHmmss"));
this.tbInfo.AppendText(string.Format("已创建文件夹{0}
", dirName));
}
/// <summary>
/// 特殊文件夹路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFolderPath_Click(object sender, EventArgs e)
{
//取得特殊文件夹的绝对路径
this.tbInfo.AppendText(string.Format("特殊文件夹路径
桌面:{0}
", Environment.GetFolderPath(Environment.SpecialFolder.Desktop)));
this.tbInfo.AppendText(string.Format("收藏夹:{0}
", Environment.GetFolderPath(Environment.SpecialFolder.Favorites)));
this.tbInfo.AppendText(string.Format("我的文档:{0}
", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)));
this.tbInfo.AppendText(string.Format("最近使用的文档:{0}
", Environment.GetFolderPath(Environment.SpecialFolder.Recent)));
//取得特殊文件夹的绝对路径
//桌面
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//收藏夹
Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
//我的文档
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//最近使用的文档
Environment.GetFolderPath(Environment.SpecialFolder.Recent);
}
/// <summary>
/// 重命名
/// </summary>
void RenameDirectory()
{
// 重命名文件夹
Computer myPC = new Computer();
string sourceDirName = @"C:UsersDellDesktop截图";
string newDirName = @"截图";
//必须是名称,而不是绝对路径
myPC.FileSystem.RenameDirectory(sourceDirName, newDirName);
myPC = null;
}
private void btnRenameDir_Click(object sender, EventArgs e)
{
RenameDirectory();
}