using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _03遍历建立文件夹
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
folderBrowserDialog.SelectedPath = @"e:\";
folderBrowserDialog.Description = "请选择一个文件夹";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
string selectedFolderPath = folderBrowserDialog.SelectedPath;
//MessageBox.Show("Selected Folder: " + selectedFolderPath);
textBox1.Text = selectedFolderPath;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
folderBrowserDialog.SelectedPath = @"e:\";
folderBrowserDialog.Description = "请选择一个文件夹";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
string selectedFolderPath = folderBrowserDialog.SelectedPath;
//MessageBox.Show("Selected Folder: " + selectedFolderPath);
textBox2.Text = selectedFolderPath;
}
}
}
private void button3_Click(object sender, EventArgs e)
{
// 源文件夹路径
string parentFolderPath = textBox1.Text.Trim();
string newFolderPath = textBox2.Text.Trim();
if (string.IsNullOrEmpty(parentFolderPath)||string.IsNullOrEmpty(newFolderPath))
{
MessageBox.Show("源&目文件夹选择不能为空...");
textBox1.Focus();
return;
}
// 确保父文件夹存在
if (!Directory.Exists(parentFolderPath))
{
MessageBox.Show("源文件夹不存在!");
return;
}
// 获取父文件夹中的所有子文件夹信息
DirectoryInfo parentDir = new DirectoryInfo(parentFolderPath);
DirectoryInfo[] subDirs = parentDir.GetDirectories(); // 获取所有子目录信息
// 遍历并打印每个子文件夹的名称
foreach (DirectoryInfo dir in subDirs)
{
//textBox3.Text+= newFolderPath+"\\"+dir.Name+"\r\n"; // 打印子文件夹名称
string tmpStr =newFolderPath+"\\"+dir.Name;
// 文件夹存在否
if (!Directory.Exists(tmpStr))
{
Directory.CreateDirectory(tmpStr);
}
}
MessageBox.Show("空文件夹从源批量创建成功...");
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
}
}
}