C# 中的文件夹结构复制

发布于 2024-12-11 07:56:32 字数 902 浏览 0 评论 0原文

我正在创建一个应用程序,我们可以在其中通过网络选择目标计算机,并将选定的源文件复制到特定文件夹中。本质上,它旨在在应用程序服务器上工作,其中一台机器将有多个应用程序服务器,在本例中为 apache tomcat。

目前,我的代码能够将一个源文件处理到特定目的地。它为该机器上存在的所有 tomcat 文件夹(tomcat1、tomcat2.. 等)执行此操作

。我使用 DirectoryInfo 来选择文件夹列表。

DirectoryInfo diTom = new DirectoryInfo(txtTomcat.Text);

其中 txtTomcat.text 是 tomcat 文件夹的网络路径。然后我使用 foreach 循环,

foreach (DirectoryInfo subDir in diTomDirs)

因此,对于目录 info 中 tomcat 的每个条目,它都会执行一个简单的 File.Copy 代码,将文件复制到为每个 tomcat 指定的文件夹内。

现在,我想扩展我的应用程序功能以考虑源文件夹,而不仅仅是文件。

例如,我有文件夹 A,包含 file1.txt 和文件夹 B。文件夹 B 又包含 file2.txt 和 file3.txt。类似的结构也会在目标 tomcat 上存在,但几乎没有其他文件夹和文件。

我想将源文件夹 A 作为源,它应该执行文件复制的现有代码,但现在,将文件从源文件夹复制到目标上的相应文件夹,即 A(source) -> A(服务器)和从 B(源)到 B(服务器)的文件。

我希望我没有让它听起来太令人困惑..:(

我想这将是我需要调整的 foreach 逻辑,但无法弄清楚如何调整。

有任何线索吗?

非常感谢,

Abhi

I am creating an application where we can select a destination machine over the network and it would copy a selected source file on a specific folder. Essentially, it's intended to work on application servers where a single machine would have multiple app servers, apache tomcat in this case.

Currently, my code is able to process one source file to a specific destination. It does it for all the tomcat folder's present on that machine(tomcat1, tomcat2.. etc..)

I am using directoryinfo to select the list of folders.

DirectoryInfo diTom = new DirectoryInfo(txtTomcat.Text);

where txtTomcat.text is the network path of the tomcat folder. Then I am using a foreach loop

foreach (DirectoryInfo subDir in diTomDirs)

Thus, for each entry of tomcat in the directory info, it executes a simple File.Copy code, copying the file inside the folder specified for each tomcat.

Now, I want to extend my applications functionality to consider source folders, instead of just file.

e.g. I have folder A, containing file1.txt and folder B. Folder B in turn contains file2.txt and file3.txt. Similar structure would also exit on the destination tomcat, but with few other folders and files.

I'd like to give the source folder A as the source, and it should execute the existing code of file copy but now, copying files from source folder to the corresponding folder on the destination, i.e. A(source) -> A(server) and files from B(source) to B(server).

I hope I didn't make it sound too confusing.. :(

I guess it would be the foreach logic which I need to tweak but not able to figure out how.

Any clues?

Many Thanks,

Abhi

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

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

发布评论

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

评论(2

允世 2024-12-18 07:56:32

更简单的方法是使用递归函数。

public static void ProcessDirectory(string targetDirectory) 
{
    foreach(string fileName in Directory.GetFiles(targetDirectory))
        ProcessFile(fileName);

    foreach(string subdirectory in Directory.GetDirectories(targetDirectory))
        ProcessDirectory(subdirectory);

    // Here is called for each directory and sub directory
}

public static void ProcessFile(string path) 
{
    // Here is called for each file in all subdirectories
}

The simpler way is to use a recursive function.

public static void ProcessDirectory(string targetDirectory) 
{
    foreach(string fileName in Directory.GetFiles(targetDirectory))
        ProcessFile(fileName);

    foreach(string subdirectory in Directory.GetDirectories(targetDirectory))
        ProcessDirectory(subdirectory);

    // Here is called for each directory and sub directory
}

public static void ProcessFile(string path) 
{
    // Here is called for each file in all subdirectories
}
楠木可依 2024-12-18 07:56:32

一种选择是使用 robocopy。它比您可能有时间投资的东西更加健壮(重试)和优化(并行)。正如 slugseter 所说,它于 2008 年发货。

我知道许多大公司使用它来推出他们的前端服务器位...

您可以从 Windows 资源工具包工具中获取它:

http://www.microsoft.com/download/en/details.aspx?id=17657

One option is to use robocopy. It is much more robust (retries) and optimal (parallel) than something you will likely have time to invest in. As slugseter noted it shipped on 2008.

I've known many large companies that use it to push out their front end server bits ...

You can get it from the windows resource kit tools:

http://www.microsoft.com/download/en/details.aspx?id=17657

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文