惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - 章立民研究室

我在點部落 笑談我的學習之路 下一代网页开发技术Silverlight——笑谈程式人生系列讲座之二 确保您的Silverlight 1.0运行时间组件是最新版本 ToolkitScriptManager VS ScriptManager 读者问与答 如何于撰写 Silverlight 1.0 的XAML时拥有Intellisense Silverlight 1.0 RC SDK 预览 新世代网站开发技术ASP .NET AJAX——章立民北京研讨会 PPT下载 新世代网站开发技术ASP .NET AJAX——章立民北京研讨会邀请函 读者询问是否一定要安装ASP.NET AJAX Extensions 答读者问 SQL Server - 请问数据库字段加密问题 读者“翔”询问如何于asp.net中删除目录 读者问与答 答读者问 Visual C# 2005 – 如何使用通配符 *.* 复制所有文件 新书出版了 SQL Server 2005 - 如何利用CLR存储过程读取与写入二进制影像数据
Visual C# - 读者询问如何复制目录以及目录下所有的子目录与文件
章立民研究室 · 2007-03-06 · via 博客园 - 章立民研究室

原发问问题:

老师:你好,新年快乐.

IO与数据存取密诀里有提到文件复制及移动目录.

但如何复制整个目录及目录下面的所有子目录及所有文件到某个地方?

还有如何使用以前*.*的通配符来复制所有文件?

谢谢.请帮忙解答

解答:

亲爱的读者您好

很感谢您对于章立民研究室的支持

有关于您提到的问题

回复如下

如果需要复制整个目录的内容到另一个目录,以Visual C#来说,最简便的方法,就是使用Microsoft.VisualBasic.Devices命名空间的My.Computer.FileSystem对象之CopyDirectory方法,它拥有下列四个多载版本(注:相关参数的用途请参阅My.Computer.FileSystem. CopyDirectory方法的说明)

public void CopyDirectory
(
 string sourceDirectoryName,
 string destinationDirectoryName
)--public void CopyDirectory
(
 string sourceDirectoryName,
 string destinationDirectoryName,
 bool overwrite
)--public void CopyDirectory
(
 string sourceDirectoryName,
 string destinationDirectoryName,
 UIOption showUI
)--public void CopyDirectory
(
 string sourceDirectoryName,
 string destinationDirectoryName,
 UIOption showUI,
 UICancelOption onUserCancel
)

请注意:

要使用Visual BasicMy对象之前,必须先加入对Microsoft.VisualBasic的参考,再汇入适当的命名空间,例如:

using Microsoft.VisualBasic.Devices;

就可以在C#中使用与My相似的语法来撰写程序。

 

图表1

如图表1所示,程序范例示范如何复制目录,兹将程序代码列示如下:

public partial class DemoForm001 : Form
{
 ...
 private void DemoForm001_Load(object sender, EventArgs e)
 {
  this.showUIComboBox.DataSource =
    System.Enum.GetNames(typeof(UIOption));
  this.showUIComboBox.SelectedIndex = 1;
 
  this.onUserCancelComboBox.DataSource =
    System.Enum.GetNames(typeof(UICancelOption));
  this.onUserCancelComboBox.SelectedIndex = 0;
 }
 
 private void FileBrowseButton_Click(object sender, EventArgs e)
 {
  FolderBrowserDialog folderDialog = new FolderBrowserDialog();
 
  folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
 
  if ((folderDialog.ShowDialog() ==
    System.Windows.Forms.DialogResult.OK))
  {
   this.FileToBeCopiedTextBox.Text = folderDialog.SelectedPath;
  }
 }
 
 private void DirectoryBrowseButton_Click(object sender, EventArgs e)
 {
  FolderBrowserDialog folderDialog = new FolderBrowserDialog();
 
  folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
 
  if ((folderDialog.ShowDialog() ==
    System.Windows.Forms.DialogResult.OK))
  {
   this.DestionFileTextBox.Text = folderDialog.SelectedPath;
  }
 }
 
 private void btnCopyFolder_Click(object sender, EventArgs e)
 {
  Computer MyComputer = new Computer();
 
  if(this.DestionFileTextBox.Text == "")
  {
   MessageBox.Show("您并未指定复制目标数据夹。", "请注意");
   this.DestionFileTextBox.Focus();
   this.DestionFileTextBox.SelectionStart =
     this.DestionFileTextBox.Text.Length;
   return;
  }
 
  try
  {
   MyComputer.FileSystem.CopyDirectory(
     this.FileToBeCopiedTextBox.Text,
     this.DestionFileTextBox.Text,
     (UIOption)(System.Enum.Parse(typeof(UIOption),
     showUIComboBox.SelectedItem.ToString())),
     (UICancelOption)(System.Enum.Parse(typeof(UICancelOption),
     onUserCancelComboBox.SelectedItem.ToString())));
  
   // 启动 Windows 文件总管。
   Process.Start("explorer.exe",
     Path.GetDirectoryName(this.DestionFileTextBox.Text));
  }
  catch (Exception ex)
  {
   MessageBox.Show(ex.Message);
  }
 }