我有多个文件夹。它们以文件扩展名命名。(例如:- doc、dwg、jpg.....等)我的列表框数据源有更多文件。(例如:- abc.dwg、beauty.jpg、arc.doc.....)我想移动文档文件到 doc 文件夹,jpg 文件到 jpg 文件夹,dwg 文件到 dwg 文件夹...等等如何做单击按钮>>“创建文件夹”按钮使用
List<string> fileNames = null;
List<string> fileExtensions = null;
private void btn_list_Click(object sender, EventArgs e)
{
listBox_ex.Items.Clear();
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)));
}
}
}
胡说叔叔
相关分类