创建多个以列表框的项目命名的文件夹
Create multiple folders named after the Items of a ListBox
单击 Get List
按钮:
- 我用选定的文件名填充一个列表框。
- 然后我将文件扩展名分开并填充另一个列表框,删除 点。
单击 Create Folders
按钮,
我想删除重复的扩展名并创建以 ListBox 中的项目命名的文件夹。
即,创建名称为 doc
、docx
、dwg
等的文件夹
private void btn_list_Click(object sender, EventArgs e)
{
listBox_ex.Items.Clear();
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
lbl_path.Text = FBD.SelectedPath;
listBox_name.Items.Clear();
string[] filename = Directory.GetFiles(FBD.SelectedPath);
foreach (string file in filename)
{
listBox_name.Items.Add(Path.GetFileName(file));
listBox_ex.Items.Add(Path.GetExtension(file).Replace(".", ""));
}
}
}
private void btn_CreateFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBD2 = new FolderBrowserDialog();
if (FBD2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
lbl_pathCreated.Text = FBD2.SelectedPath;
}
string path = lbl_pathCreated.Text;
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
} else {
MessageBox.Show("already exit");
}
}
替代方法,使用 2 List<string>
对象将当前文件的选择和文件扩展名存储为 Distinct, Ordered 元素。
然后将列表用作 ListBox.DataSource。
ListBox.Items
收集器在 DataSource 更改时重置。
选择目标路径后,扩展列表中的每一项都用于在所选路径中创建目录。
不需要检查目录是否已经存在:Directory.CreateDirectory() 只是忽略它。
您可能想要为用户选择的路径添加验证程序。用户可能选择了一个 奇怪的 目的地。
List<string> fileNames = null;
List<string> fileExtensions = null;
private void btn_list_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
if (fbd.ShowDialog() == DialogResult.OK)
{
lbl_path.Text = fbd.SelectedPath;
fileNames = Directory.GetFiles(fbd.SelectedPath).ToList();
fileExtensions = fileNames.Select(item =>
Path.GetExtension(item).Replace(".", "")).Distinct().OrderBy(n => n).ToList();
listBox_name..DataSource = fileNames.Select(f => Path.GetFileName(f)).ToList();
listBox_ex.DataSource = fileExtensions;
}
}
}
private void btn_CreateFolder_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
if (fbd.ShowDialog() == DialogResult.OK)
{
lbl_pathCreated.Text = fbd.SelectedPath;
fileExtensions.ForEach(item =>
Directory.CreateDirectory(Path.Combine(fbd.SelectedPath, item)));
}
}
}
单击 Get List
按钮:
- 我用选定的文件名填充一个列表框。
- 然后我将文件扩展名分开并填充另一个列表框,删除 点。
单击 Create Folders
按钮,
我想删除重复的扩展名并创建以 ListBox 中的项目命名的文件夹。
即,创建名称为 doc
、docx
、dwg
等的文件夹
private void btn_list_Click(object sender, EventArgs e)
{
listBox_ex.Items.Clear();
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
lbl_path.Text = FBD.SelectedPath;
listBox_name.Items.Clear();
string[] filename = Directory.GetFiles(FBD.SelectedPath);
foreach (string file in filename)
{
listBox_name.Items.Add(Path.GetFileName(file));
listBox_ex.Items.Add(Path.GetExtension(file).Replace(".", ""));
}
}
}
private void btn_CreateFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBD2 = new FolderBrowserDialog();
if (FBD2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
lbl_pathCreated.Text = FBD2.SelectedPath;
}
string path = lbl_pathCreated.Text;
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
} else {
MessageBox.Show("already exit");
}
}
替代方法,使用 2 List<string>
对象将当前文件的选择和文件扩展名存储为 Distinct, Ordered 元素。
然后将列表用作 ListBox.DataSource。
ListBox.Items
收集器在 DataSource 更改时重置。
选择目标路径后,扩展列表中的每一项都用于在所选路径中创建目录。
不需要检查目录是否已经存在:Directory.CreateDirectory() 只是忽略它。
您可能想要为用户选择的路径添加验证程序。用户可能选择了一个 奇怪的 目的地。
List<string> fileNames = null;
List<string> fileExtensions = null;
private void btn_list_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
if (fbd.ShowDialog() == DialogResult.OK)
{
lbl_path.Text = fbd.SelectedPath;
fileNames = Directory.GetFiles(fbd.SelectedPath).ToList();
fileExtensions = fileNames.Select(item =>
Path.GetExtension(item).Replace(".", "")).Distinct().OrderBy(n => n).ToList();
listBox_name..DataSource = fileNames.Select(f => Path.GetFileName(f)).ToList();
listBox_ex.DataSource = fileExtensions;
}
}
}
private void btn_CreateFolder_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
if (fbd.ShowDialog() == DialogResult.OK)
{
lbl_pathCreated.Text = fbd.SelectedPath;
fileExtensions.ForEach(item =>
Directory.CreateDirectory(Path.Combine(fbd.SelectedPath, item)));
}
}
}