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 5 years have passed since last update.

Visual Studio | WPF > fileIO > フォルダをコピーする > File.Copy()使用

Last updated at Posted at 2017-11-15
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

特定のフォルダから別のフォルダにコピーするというのは時々使う。
その時は「ある指定の日時以降のファイル」などの条件が付くため、単純なフォルダコピーにはしない。
c++ builder / fileIO > 指定日付以降のファイルをフォルダ単位でコピーする実装

WPFにて実装してみた。

参考

Directoryクラス @ MSDN
CreateDirectory()を使う。

Visual Studio | WPF > fileIO > ファイルのコピー (上書きあり|上書きなし) > System.IO.Fileクラス使用

対象フォルダ例

D:\FolderSearch_171114は以下となっている。

┗DIR_A
  ┣171010.csv
  ┣171010.json
  ┣171011.csv
  ┣171011.json
  ┣171012.csv
  ┗171012.json

DIR_AをDIR_Cにコピーする。

code

MainWindow.xaml
<Window x:Class="_171115_t1035_folderCopy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_171115_t1035_folderCopy"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="B_copy" Height="30" Content="copy folder" Click="B_copy_Click"/>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// 以下を追加
using System.IO;

namespace _171115_t1035_folderCopy
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private readonly string kBaseFolder = "D:\\FolderSearch_171114";
        private readonly string kFromSubFolder = "DIR_A";
        private readonly string kToSubFolder = "DIR_C";

        private void copyFolder(string fromFolder, string toFolder)
        {
            // 1. create folder
            if (Directory.Exists(toFolder) == false)
            {
                Directory.CreateDirectory(toFolder);
            }

            // 2. copy files
            IEnumerable<string> fileEmrt = Directory.EnumerateFiles(fromFolder, "*.*", SearchOption.TopDirectoryOnly);
            foreach(var fromFile in fileEmrt)
            {
                string toFile = fromFile.Replace(kFromSubFolder, kToSubFolder);
                Console.WriteLine(toFile);
                try
                {
                    File.Copy(fromFile, toFile, overwrite: true);
                } catch(Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }            
        }

        private void B_copy_Click(object sender, RoutedEventArgs e)
        {
            string fromFolder = $"{kBaseFolder}\\{kFromSubFolder}";  // C#6.0以降
            string toFolder = $"{kBaseFolder}\\{kToSubFolder}"; // C#6.0以降
            copyFolder(fromFolder, toFolder);
        }
    }
}

qiita.png

ボタンを押したらDIR_Cのコピーができた。

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?