欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#使用file.copy实现文件备份示例

程序员文章站 2023-12-20 12:07:40
步骤:1、遍历d盘source文件夹找出所有名称包含lte的文件,文件路径存放到list中2、遍历list,把所有文...

步骤:

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;
            }
        }
    }
}

上一篇:

下一篇: