Visual C# - 读者询问如何复制目录以及目录下所有的子目录与文件
2007-03-06 09:50
666 查看
原发问问题:
章老师:你好,新年快乐.
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 Basic的My对象之前,必须先加入对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);
}
}
章老师:你好,新年快乐.
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 Basic的My对象之前,必须先加入对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);
}
}
相关文章推荐
- Visual C# - 读者询问如何复制目录以及目录下所有的子目录与文件
- 如何复制一个目录里面的所有目录和文件
- Java实现——字符串分割以及复制目录下的所有文件
- 批处理:复制目录A及其A子目录里的文件到A并删掉所有子目录
- 遍历指定目录下的所有文件和目录, 以及子目录下的文件和目录
- 如何touch一个目录及其下所有嵌套子目录中的文件?
- [批处理]把指定文件复制到指定目录下的所有子目录下(包括子目录的下级目录)
- Java实现——字符串分割以及复制目录下的所有文件
- Visual C# 2005 – 如何使用通配符 *.* 复制所有文件
- 编写一个Python程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
- 遍历该目录以及子目录下所有的文件
- linux 命令 改变指定目录以及其子目录下的所有文件的拥有者和群组
- 【代码】当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出绝对路径
- 递归解压当前目录以及子目录下的所有压缩文件,并删除源文件
- Visual C# 2005 – 如何使用通配符 *.* 复制所有文件
- Java-->用递归方法复制目录、子目录以及文件(文件用到IO流)
- 如何列出当前目录下所有的JSP文件(含子目录)
- chmod +x *.sh 这个命令是为sh文件增加可执行权限; chmod +R 777 *.* 对当前目录下的所有子目录和子文件进行 777权限的变更;通在安装软件时复制相关文件。
- linux下复制目录中所有文件及子目录(包括隐藏文件)
- 【IO面试题】文件操作,如何列出某个目录下的所有文件?如何列出某个目录下的所有子目录?写个简单的例子