TQC+ C# 認證教學手冊

210、檔案同步

File System API

List Files

DirectoryInfo windir = new DirectoryInfo("C:\\Windows");

foreach (FileInfo file in windir.EnumerateFiles())
{
    Console.WriteLine(file.Name);
    Console.WriteLine(file.FullName);
    Console.WriteLine(file.LastAccessTime);
    Console.WriteLine(file.LastWriteTime);
}

Path

string path = "C:\\Windows\\notepad.exe";

Console.WriteLine(Path.GetDirectoryName(path));
Console.WriteLine(Path.GetFileName(path));
Console.WriteLine(Path.GetExtension(path));

Combine

string dir = "C:\\Windows";
string file = "notepad.exe";

Console.WriteLine(Path.Combine(dir, file));

刪除資料夾

rmdir /s NewData

參考程式碼

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Security.AccessControl;

namespace CSD02
{
    class Program
    {
        static void Main(string[] args)
        {
            //判斷使用者參數是否有輸入
            if (args.Length < 2)
            {
                Console.WriteLine("輸入參數: <舊目錄資料> <新目錄資料>");
                return;
            }
            else
            {
                string source = args[0];
                string dest = args[1];

                //同步目錄
                SyncFolder(source, dest);
            }
            Console.ReadLine();
        }
        static public void SyncFolder(string sourceFolder, string destFolder)
        {
            //使用遞回方式把來源目錄複製到目的目錄
            //來源資料不存在
            if (!Directory.Exists(sourceFolder))
            {
                //檢查舊目錄是否存在
                Console.WriteLine("來源目錄不存在");
                return;
            }

            if (!Directory.Exists(destFolder))
            {
                //設定目的目錄給everyone讀取權限
                Directory.CreateDirectory(destFolder);
                DirectorySecurity dirSecurity = Directory.GetAccessControl(destFolder);
                FileSystemAccessRule guestRule = new FileSystemAccessRule("everyone", FileSystemRights.Read, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
                dirSecurity.AddAccessRule(guestRule);
                dirSecurity.SetAccessRule(guestRule);
                Directory.SetAccessControl(destFolder, dirSecurity);
                Console.WriteLine(string.Format("新增目錄:{0}\r\n", destFolder));
            }

            DirectoryInfo directory = new DirectoryInfo(sourceFolder);
            IEnumerable<FileInfo> files = directory.EnumerateFiles();
            foreach (var file in files)
            {
                string name = Path.GetFileName(file.FullName);
                string dest = Path.Combine(destFolder, name);

                //判斷來源檔案是否有大於目的檔如果超過就複製,若目的檔不存在就複製一份
                if (File.Exists(dest))
                {
                    FileInfo destFile = new FileInfo(dest);
                    if (file.LastWriteTime > destFile.LastWriteTime)
                    {
                        file.CopyTo(dest, true);
                        Console.WriteLine("修改檔案:{0}", dest);
                        Console.WriteLine("大小:{0:0.00} KB", file.Length / 1024.0);
                        Console.WriteLine();
                    }
                }
                else
                {
                    file.CopyTo(dest, false);
                    Console.WriteLine("新增檔案:{0}", dest);
                    Console.WriteLine("大小:{0:0.00} KB", file.Length / 1024.0);
                    Console.WriteLine();
                }
            }

            IEnumerable<string> folders = Directory.EnumerateDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);

                SyncFolder(folder, dest);
            }
        }
    }
}