从路径的一部分创建具有文件夹结构的子目录

发布于 2024-07-26 10:02:09 字数 337 浏览 8 评论 0原文

如果我有一条路径,就像这样:

c:\windows\system32\inetsrv\abc.exe

我想在驱动器/路径下创建一个文件夹,例如

Z:\东西\

如何创建子目录并选择是否需要 windows 及以上、system32 及以上或 inetsrv 及以上的所有内容? 顺便说一句,我知道 DirectoryInfo 有一个名为 CreateSubDirectory() 的方法。

谢谢

If I have a path, like so:

c:\windows\system32\inetsrv\abc.exe

And I want to make a folder under a drive/path such as

Z:\Stuff\

How can I make a sub directory and select whether I want everything from windows onwards, system32 onwards, or inetsrv onwards? BTW I know DirectoryInfo has a method called CreateSubDirectory().

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑脸一如从前 2024-08-02 10:02:09

您可以使用 System.IO.Directory.CreateDirectory(path)。 它是一种静态方法,与 DirectoryInfo 不同,您需要目录的实例才能调用它。

之后,您可以使用 Xcopy 或 Robocopy 复制所有文件和子目录。

You can use System.IO.Directory.CreateDirectory(path). It is a static method unlike the DirectoryInfo one where you need an instance of a directory to call it.

After that you could use Xcopy or Robocopy to copy all the files and subdirectories.

与君绝 2024-08-02 10:02:09

如果您想将所有内容从一个目录复制到另一个目录,可以使用如下命令:

using System;
using System.Collections.Generic;
using System.IO;

namespace so_cp__r
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter path from where you want to copy:");
            List<DirectoryInfo> fromDirs = new List<DirectoryInfo>();
            DirectoryInfo dir = new FileInfo(Console.ReadLine()).Directory;
            while (dir != null)
            {
                fromDirs.Add(dir);
                dir = dir.Parent;
            }
            for (int i = 0; i < fromDirs.Count; i++)
                Console.WriteLine("{0}: {1}", i, fromDirs[i].FullName);
            Console.WriteLine("From which of these dirs do you want to start?");
            DirectoryInfo fromDir = fromDirs[int.Parse(Console.ReadLine())];
            Console.WriteLine("Where do you want to copy?");
            DirectoryInfo toDir = new DirectoryInfo(Console.ReadLine());
            recursive_copy(fromDir, toDir);
        }

        static void recursive_copy(DirectoryInfo fromDir, DirectoryInfo toDir)
        {
            DirectoryInfo child = toDir.CreateSubdirectory(fromDir.Name);
            foreach (FileInfo file in fromDir.GetFiles())
                file.CopyTo(child.FullName + Path.DirectorySeparatorChar + file.Name);
            foreach (DirectoryInfo dir in fromDir.GetDirectories())
                recursive_copy(dir, child);
        }
    }
}

If you want to copy everything from one directory to another you can use something like this:

using System;
using System.Collections.Generic;
using System.IO;

namespace so_cp__r
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter path from where you want to copy:");
            List<DirectoryInfo> fromDirs = new List<DirectoryInfo>();
            DirectoryInfo dir = new FileInfo(Console.ReadLine()).Directory;
            while (dir != null)
            {
                fromDirs.Add(dir);
                dir = dir.Parent;
            }
            for (int i = 0; i < fromDirs.Count; i++)
                Console.WriteLine("{0}: {1}", i, fromDirs[i].FullName);
            Console.WriteLine("From which of these dirs do you want to start?");
            DirectoryInfo fromDir = fromDirs[int.Parse(Console.ReadLine())];
            Console.WriteLine("Where do you want to copy?");
            DirectoryInfo toDir = new DirectoryInfo(Console.ReadLine());
            recursive_copy(fromDir, toDir);
        }

        static void recursive_copy(DirectoryInfo fromDir, DirectoryInfo toDir)
        {
            DirectoryInfo child = toDir.CreateSubdirectory(fromDir.Name);
            foreach (FileInfo file in fromDir.GetFiles())
                file.CopyTo(child.FullName + Path.DirectorySeparatorChar + file.Name);
            foreach (DirectoryInfo dir in fromDir.GetDirectories())
                recursive_copy(dir, child);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文