C#实现文件Move操作和文件的Copy操作

2022-10-11,,,

文件移动(move)操作和文件的复制(copy)是c#程式开发经常遇到的方法,根据传入的源文件地址和目标文件地址参数,实现对文件的操作。实现代码如下:

  1. move操作代码:
    public static void movefolder(string sourcepath, string destpath)
            {
                if (directory.exists(sourcepath))
                {
                    if (!directory.exists(destpath))
                    {
                        //目标目录不存在则创建  
                        try
                        {
                            directory.createdirectory(destpath);
                        }
                        catch (exception ex)
                        {
                            throw new exception("创建目标目录失败:" + ex.message);
                        }
                    }
                    //获得源文件下所有文件  
                    list<string> files = new list<string>(directory.getfiles(sourcepath));
                    files.foreach(c =>
                    {
                        string destfile = path.combine(new string[] { destpath, path.getfilename(c) });
                        //覆盖模式  
                        if (file.exists(destfile))
                        {
                            file.delete(destfile);
                        }
                        file.move(c, destfile);
                    });
                    //获得源文件下所有目录文件  
                    list<string> folders = new list<string>(directory.getdirectories(sourcepath));
    
                    folders.foreach(c =>
                    {
                        string destdir = path.combine(new string[] { destpath, path.getfilename(c) });
                        //directory.move必须要在同一个根目录下移动才有效,不能在不同卷中移动。  
                        //directory.move(c, destdir);  
    
                        //采用递归的方法实现  
                        movefolder(c, destdir);
                    });
                }
                else
                {
           
  2. copy操作代码:
    public static void copyfilefolder(string sourcefilepath, string targetfilepath)
            {
                //获取源文件夹中的所有非目录文件
                string[] files = directory.getfiles(sourcefilepath);
                string filename;
                string destfile;
                //如果目标文件夹不存在,则新建目标文件夹
                if (!directory.exists(targetfilepath))
                {
                    directory.createdirectory(targetfilepath);
                }
                //将获取到的文件一个一个拷贝到目标文件夹中  
                foreach (string s in files)
                {
                    filename = path.getfilename(s);
                    destfile = path.combine(targetfilepath, filename);
                    file.copy(s, destfile, true);
                }
                //上面一段在msdn上可以看到源码 
    
                //获取并存储源文件夹中的文件夹名
                string[] filefolders = directory.getfiles(sourcefilepath);
                //创建directoryinfo实例 
                directoryinfo dirinfo = new directoryinfo(sourcefilepath);
                //获取得源文件夹下的所有子文件夹名
                directoryinfo[] subfilefolder = dirinfo.getdirectories();
                for (int j = 0; j < subfilefolder.length; j++)
                {
                    //获取所有子文件夹名 
                    string subsourcepath = sourcefilepath + "\\" + subfilefolder[j].tostring();
                    string subtargetpath = targetfilepath + "\\" + subfilefolder[j].tostring();
                    //把得到的子文件夹当成新的源文件夹,递归调用copyfilefolder
                    copyfilefolder(subsourcepath, subtargetpath);
                }
            }

     

《C#实现文件Move操作和文件的Copy操作.doc》

下载本文的Word格式文档,以方便收藏与打印。