مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

ahmadi

عضویت از 1393/07/18

کپی محتویات پوشه به پوشه دیگر

  • سه شنبه 3 بهمن 1396
  • 20:37
تشکر میکنم

سلام خدمت دوستان

از طریق کد زیر محتویات پوشه به پوشه دیگر کپی میکنم:

 string[] list = Directory.GetFiles(source_dir);
            try
            {
                foreach (string files in list)
                {

                    string name = files.Substring(source_dir.Length + 1);
                    File.Copy(Path.Combine(source_dir, name), Path.Combine(destination_dir, name));
                }
            }
            catch (Exception)
            {
                //کد جایگزین replace
                foreach (string files in list)
                {
                    string name = files.Substring(source_dir.Length + 1);

                    File.Copy(Path.Combine(source_dir, name), Path.Combine(destination_dir, name), true);
                }
            }

اگه داخل اون پوشه پوشه دیگری هم باشد پوشه داخلی کپی نمی شود

فایل هایی که درون تصویر بالا مشاهده میکنید فایلها کپی می شوند اما پوشه ها (css,js,image,user) کپی نمی شود

پاسخ های این پرسش

تعداد پاسخ ها : 1 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • سه شنبه 3 بهمن 1396
  • 22:32
using System;
using System.IO;

class DirectoryCopyExample
{
    static void Main()
    {
        // Copy from the current directory, include subdirectories.
        DirectoryCopy(".", @".\temp", true);
    }

    private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }
        
        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
        }

        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
}

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)