程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#應用file.copy完成文件備份示例

c#應用file.copy完成文件備份示例

編輯:C#入門知識

c#應用file.copy完成文件備份示例。本站提示廣大學習愛好者:(c#應用file.copy完成文件備份示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#應用file.copy完成文件備份示例正文


步調:

1、遍歷D盤Source文件夾找出一切稱號包括LTE的文件,文件途徑寄存到List<string>中
2、遍歷List<string>,把一切文件Copy到E盤的備份文件夾中


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.IO;

namespace CopyLTE
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string txtPath = @"..\..\Dir\directory.txt";
                string srcPath = "";
                string aimPath = "";
                string KeyWords = "";
                List<string> KeyFloderList = new List<string>();
                if (File.Exists(txtPath) == false)
                {
                    Console.WriteLine("directory.txt不存在,無法獲取Copy路徑");
                }
                else
                {
                    GetCopyPath(txtPath, out srcPath, out aimPath, out KeyWords);
                    if (srcPath == "" || aimPath == "" || KeyWords == "")
                    {
                        Console.WriteLine("請在directory.txt,輸入Copy的源文件夾,目標文件,和KeyWords");
                    }
                    else
                    {
                        DirectoryInfo FolderSource = new DirectoryInfo(srcPath);
                        foreach (DirectoryInfo NextFolder in FolderSource.GetDirectories())
                        {
                            if (NextFolder.FullName.Contains(KeyWords))
                            {
                                KeyFloderList.Add(NextFolder.FullName);
                            }
                        }
                    }
                }
                if (KeyFloderList.Count > 0)
                {
                    foreach (string FloderPath in KeyFloderList)
                    {
                        //Copy From Source To Backup
                        CopyDirectory(FloderPath, aimPath);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //
        private static void CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);

            string desfolderdir = desdir + "\\" + folderName;

            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);

            foreach (string file in filenames)// 遍歷一切的文件和目次
            {
                if (Directory.Exists(file))// 先看成目次處置假如存在這個目次就遞歸Copy該目次上面的文件
                {

                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }

                    CopyDirectory(file, desfolderdir);
                }

                else // 不然直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                    srcfileName = desfolderdir + "\\" + srcfileName;
                    if (File.Exists(srcfileName))
                    {
                        File.Delete(srcfileName); //Delete Old Files
                    }

                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }
                    File.Copy(file, srcfileName);
                }
            }
        }
        //獲得路徑
        public static void GetCopyPath(string strTxt, out string From, out string To, out string keyWords)
        {
            try
            {
                string[] stringLines = File.ReadAllLines(strTxt, Encoding.Default);
                if (stringLines.Length >= 3)
                {
                    From = stringLines[0].ToString();
                    To = stringLines[1].ToString();
                    keyWords = stringLines[2].ToString();
                }
                else
                {
                    From = "";
                    To = "";
                    keyWords = "";
                    Console.WriteLine("請在directory.txt,輸入Copy的源文件夾,目標文件,和KeyWords");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved