亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將文件移動到多個文件夾?

如何將文件移動到多個文件夾?

C#
守著一只汪 2022-12-31 13:14:29
我有多個文件夾。它們以文件擴展名命名。(例如:- 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)));        }    }}
查看完整描述

1 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

簡短的回答是您只需調用File.Move, 并將現有文件的完整路徑作為第一個參數傳遞,并傳遞目標的完整路徑和文件名。


您可以構建目標路徑,然后像這樣移動文件:


foreach (string file in fileNames)

{

    // Build the destination path

    var destination = Path.Combine(

        FBD.SelectedPath,                           // The root destination folder

        Path.GetExtension(file).Replace(".", ""),   // The file extension folder

        Path.GetFileName(file));                    // The file name (including extension)


    // Move the file

    File.Move(file, destination);

}


查看完整回答
反對 回復 2022-12-31
  • 1 回答
  • 0 關注
  • 182 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號