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