C# 将文件从一个位置复制到另一个位置

C# 将文件从一个位置复制到另一个位置,c#,asp.net,C#,Asp.net,我试图创建一个目录和子目录,并将文件从一个位置复制到另一个位置。下面的代码可以工作,但如果有子目录,它不会创建父目录(10_new)。我正在尝试将所有内容(包括子目录)从“c:\\sourceLoc\\10”复制到“c:\\destLoc\\10\u new”文件夹。如果“10_new”不存在,那么我应该创建此文件夹。请帮忙 string sourceLoc = "c:\\sourceLoc\\10"; string destLoc = "c:\\destLoc\\10_new"; forea

我试图创建一个目录和子目录,并将文件从一个位置复制到另一个位置。下面的代码可以工作,但如果有子目录,它不会创建父目录(10_new)。我正在尝试将所有内容(包括子目录)从
“c:\\sourceLoc\\10”
复制到
“c:\\destLoc\\10\u new”
文件夹。如果
“10_new”
不存在,那么我应该创建此文件夹。请帮忙

string sourceLoc = "c:\\sourceLoc\\10";
string destLoc = "c:\\destLoc\\10_new";

foreach (string dirPath in Directory.GetDirectories(sourceLoc, "*", SearchOption.AllDirectories))
{
    Directory.CreateDirectory(dirPath.Replace(sourceLoc, destLoc));
    if (Directory.Exists(sourceLoc))
    {
         //Copy all the files
         foreach (string newPath in Directory.GetFiles(sourceLoc, "*.*", SearchOption.AllDirectories))
             File.Copy(newPath, newPath.Replace(sourceLoc, destLoc));
    }
}

在执行File.Copy之前,请检查以确保文件夹存在。如果它没有创建它。 此函数将检查路径是否存在,如果不存在,将创建路径。如果它无法创建它,无论出于什么原因,它都将返回false。否则,这是真的

 Private Function checkDir(ByVal path As String) As Boolean
        Dim dir As New DirectoryInfo(path)
        Dim exist As Boolean = True
        If Not dir.Exists Then
            Try
                dir.Create()
            Catch ex As Exception
                exist = False
            End Try
        End If
        Return exist
    End Function

请记住,所有.Net语言都会编译到CLR(公共语言运行时),因此这在VB.Net或C#中并不重要。在两者之间进行转换的一个好方法是:

下面介绍如何将一个目录中的所有文件复制到另一个目录

这是从


通过查看代码,您永远不会检查父文件夹是否存在。您可以首先获取所有子文件夹

if (!Directory.Exists(@"C:\my\dir")) Directory.CreateDirectory(@"C:\my\dir");

在Windows7中,用C#复制或移动文件是不可能的


它将创建一个零字节的文件。

Idk如果已经有easy peasy库函数,但您可以递归地检查子文件夹并跨文件夹复制每个子文件夹。请检查问题是关于C#,而不是VBall.Net语言编译到CLR,并且可以在语言之间轻松转换()。此外,这些问题并不依赖于语言,他们也不询问语法。我同意jason的观点。不管你用什么语言,你都应该明白代码旁边的逻辑是什么!谢谢,如果你同意的话,我将非常感谢你投赞成票,如果你也同意的话,我将取消反对票Forte@jason,您只能在编辑答案的情况下撤消下一票。下一票不是复制子文件夹图像。@Billy直接从上面开始:要递归遍历当前目录下的所有子文件夹,请参阅“如何:遍历目录树。
public class RecursiveFileSearch
{
    static System.Collections.Specialized.StringCollection log = new System.Collections.Specialized.StringCollection();

    static void Main()
    {
        // Start with drives if you have to search the entire computer.
        string[] drives = System.Environment.GetLogicalDrives();

        foreach (string dr in drives)
        {
            System.IO.DriveInfo di = new System.IO.DriveInfo(dr);

            // Here we skip the drive if it is not ready to be read. This
            // is not necessarily the appropriate action in all scenarios.
            if (!di.IsReady)
            {
                Console.WriteLine("The drive {0} could not be read", di.Name);
                continue;
            }
            System.IO.DirectoryInfo rootDir = di.RootDirectory;
            WalkDirectoryTree(rootDir);
        }

        // Write out all the files that could not be processed.
        Console.WriteLine("Files with restricted access:");
        foreach (string s in log)
        {
            Console.WriteLine(s);
        }
        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key");
        Console.ReadKey();
    }

    static void WalkDirectoryTree(System.IO.DirectoryInfo root)
    {
        System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo[] subDirs = null;

        // First, process all the files directly under this folder
        try
        {
            files = root.GetFiles("*.*");
        }
        // This is thrown if even one of the files requires permissions greater
        // than the application provides.
        catch (UnauthorizedAccessException e)
        {
            // This code just writes out the message and continues to recurse.
            // You may decide to do something different here. For example, you
            // can try to elevate your privileges and access the file again.
            log.Add(e.Message);
        }

        catch (System.IO.DirectoryNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }

        if (files != null)
        {
            foreach (System.IO.FileInfo fi in files)
            {
                // In this example, we only access the existing FileInfo object. If we
                // want to open, delete or modify the file, then
                // a try-catch block is required here to handle the case
                // where the file has been deleted since the call to TraverseTree().
                Console.WriteLine(fi.FullName);
            }

            // Now find all the subdirectories under this directory.
            subDirs = root.GetDirectories();

            foreach (System.IO.DirectoryInfo dirInfo in subDirs)
            {
                // Resursive call for each subdirectory.
                WalkDirectoryTree(dirInfo);
            }
        }            
    }
}
if (!Directory.Exists(@"C:\my\dir")) Directory.CreateDirectory(@"C:\my\dir");