0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# ファイルを複数移動する方法

Posted at
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace for文2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string folderFrom = @"C:\Users\8100825\Desktop\A"; //移動元のフォルダー
            string folderTo = @"C:\Users\8100825\Desktop\B"; //移動先のフォルダー


            foreach(string pathFrom in System.IO.Directory.EnumerateFiles(folderFrom))
            {

                //移動先のパスを生成
                string pathTo = pathFrom.Replace(folderFrom, folderTo);

                //移動先のフォルダーが存在するか確認し、なければ作成します
                string targetFolder = System.IO.Path.GetDirectoryName(pathTo);
                if (System.IO.Directory.Exists(targetFolder) == false)
                {
                    System.IO.Directory.CreateDirectory(targetFolder);
                }

                //1ファイルの移動実行。同名のファイルがある場合は上書きします
                System.Diagnostics.Debug.WriteLine("移動" + pathFrom + "→" + pathTo);
                System.IO.File.Move(pathFrom, pathTo);
            }

        }
    }
}

「開始」→「button1」を押すとAフォルダにあるファイルがBフォルダに全て移動する

参考サイト
https://www.umayadia.com/cssample/sample0201/Sample260MiltipleFileMove.htm?pos=0

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?