C# 中的文件夹结构复制
我正在创建一个应用程序,我们可以在其中通过网络选择目标计算机,并将选定的源文件复制到特定文件夹中。本质上,它旨在在应用程序服务器上工作,其中一台机器将有多个应用程序服务器,在本例中为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更简单的方法是使用递归函数。
The simpler way is to use a recursive function.
一种选择是使用 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