创建,删除和移动文件夹以及文件夹列表
2008-06-19 16:47
211 查看
使用Directory类和DirectoryInfo类
一:创建文件夹
try
{
if (System.IO.Directory.Exists(DirectoryTextBox.Text))
{
MsgLabel.Text = "该文件夹已经存在";
return;
}
else
{
System.IO.DirectoryInfo dirinfo = System.IO.Directory.CreateDirectory(DirectoryTextBox.Text);
MsgLabel.Text = "成功创建该文件夹!创建时间为:" + System.IO.Directory.GetCreationTime(DirectoryTextBox.Text);
}
}
catch (Exception ee)
{
MsgLabel.Text = "处理失败! 失败的原因是:" + ee.ToString();
}
二:删除文件夹
try
{
if (!Directory.Exists(DirectoryTextBox.Text))
{
MsgLabel.Text = "该文件不存在";
}
else
{
Directory.Delete(DirectoryTextBox.Text);
MsgLabel.Text = "删除文件成功!";
}
}
catch (Exception ee)
{
MsgLabel.Text = "操作失败! 失败的原因是:" + ee.ToString();
}
三:移动文件夹
源文件夹和目标文件夹要求存在于同一个硬盘分区中否则会操作失败(操作失败! 失败原因:System.IO.IOException: 源路径和目标路径必须具有相同的根。移动操作在卷之间无效。 在 System.IO.Directory.Move(String sourceDirName, String destDirName) 在 CreateDirectory.MoveButton_Click(Object sender, EventArgs e) )
try
{
if (!System.IO.Directory.Exists(SDirectoryTextBox.Text))
{
Label1.Text = "源文件夹不存在!";
return;
}
if (System.IO.Directory.Exists(DDirectoryTextBox.Text))
{
Label1.Text = "目标文件夹已经存在!";
return;
}
System.IO.Directory.Move(SDirectoryTextBox.Text, DDirectoryTextBox.Text);
Label1.Text = "文件夹移动成功! 源文件已经被移除。目标文件夹为" + DFileTextBox.Text;
}
catch (Exception ee)
{
Label1.Text = "操作失败! 失败原因:" + ee.ToString();
}
四:文件夹列表
<table border="1">
<tr>
<td colspan="2" style="color: #660066;">
文件夹中文件列表:
</td>
</tr>
<tr>
<td style="font-size: 10pt">
请输入要删除文件的路径:
</td>
<td style="width: 158px">
<asp:TextBox ID="DirectoryTextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="MsgLabel" runat="server" ForeColor="red"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" style="width: 158px">
<asp:ListBox ID="FileListBox" runat="server" Height="192px" Width="184px"></asp:ListBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="ExistButton" runat="server" Text="确定" OnClick="ExistButton_Click" />
</td>
</tr>
</table>
try
{
if (!Directory.Exists(DirectoryTextBox.Text))
{
MsgLabel.Text = "该文件不存在";
FileListBox.Visible = false;
return;
}
else
{
DirectoryInfo dirinfo = new DirectoryInfo(DirectoryTextBox.Text);
FileSystemInfo[] dirs = dirinfo.GetFileSystemInfos();
if (dirs.Length < 1)
{
MsgLabel.Text = "该文件夹为空!";
FileListBox.Visible = false;
}
else
{
MsgLabel.Text = "该文件夹包含的文件和目录列表如下:";
FileListBox.Visible = true;
FileListBox.DataSource = dirs;
FileListBox.DataBind();
}
}
}
catch (Exception ee)
{
MsgLabel.Text = "操作失败! 失败的原因是:" + ee.ToString();
}
一:创建文件夹
try
{
if (System.IO.Directory.Exists(DirectoryTextBox.Text))
{
MsgLabel.Text = "该文件夹已经存在";
return;
}
else
{
System.IO.DirectoryInfo dirinfo = System.IO.Directory.CreateDirectory(DirectoryTextBox.Text);
MsgLabel.Text = "成功创建该文件夹!创建时间为:" + System.IO.Directory.GetCreationTime(DirectoryTextBox.Text);
}
}
catch (Exception ee)
{
MsgLabel.Text = "处理失败! 失败的原因是:" + ee.ToString();
}
二:删除文件夹
try
{
if (!Directory.Exists(DirectoryTextBox.Text))
{
MsgLabel.Text = "该文件不存在";
}
else
{
Directory.Delete(DirectoryTextBox.Text);
MsgLabel.Text = "删除文件成功!";
}
}
catch (Exception ee)
{
MsgLabel.Text = "操作失败! 失败的原因是:" + ee.ToString();
}
三:移动文件夹
源文件夹和目标文件夹要求存在于同一个硬盘分区中否则会操作失败(操作失败! 失败原因:System.IO.IOException: 源路径和目标路径必须具有相同的根。移动操作在卷之间无效。 在 System.IO.Directory.Move(String sourceDirName, String destDirName) 在 CreateDirectory.MoveButton_Click(Object sender, EventArgs e) )
try
{
if (!System.IO.Directory.Exists(SDirectoryTextBox.Text))
{
Label1.Text = "源文件夹不存在!";
return;
}
if (System.IO.Directory.Exists(DDirectoryTextBox.Text))
{
Label1.Text = "目标文件夹已经存在!";
return;
}
System.IO.Directory.Move(SDirectoryTextBox.Text, DDirectoryTextBox.Text);
Label1.Text = "文件夹移动成功! 源文件已经被移除。目标文件夹为" + DFileTextBox.Text;
}
catch (Exception ee)
{
Label1.Text = "操作失败! 失败原因:" + ee.ToString();
}
四:文件夹列表
<table border="1">
<tr>
<td colspan="2" style="color: #660066;">
文件夹中文件列表:
</td>
</tr>
<tr>
<td style="font-size: 10pt">
请输入要删除文件的路径:
</td>
<td style="width: 158px">
<asp:TextBox ID="DirectoryTextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="MsgLabel" runat="server" ForeColor="red"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" style="width: 158px">
<asp:ListBox ID="FileListBox" runat="server" Height="192px" Width="184px"></asp:ListBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="ExistButton" runat="server" Text="确定" OnClick="ExistButton_Click" />
</td>
</tr>
</table>
try
{
if (!Directory.Exists(DirectoryTextBox.Text))
{
MsgLabel.Text = "该文件不存在";
FileListBox.Visible = false;
return;
}
else
{
DirectoryInfo dirinfo = new DirectoryInfo(DirectoryTextBox.Text);
FileSystemInfo[] dirs = dirinfo.GetFileSystemInfos();
if (dirs.Length < 1)
{
MsgLabel.Text = "该文件夹为空!";
FileListBox.Visible = false;
}
else
{
MsgLabel.Text = "该文件夹包含的文件和目录列表如下:";
FileListBox.Visible = true;
FileListBox.DataSource = dirs;
FileListBox.DataBind();
}
}
}
catch (Exception ee)
{
MsgLabel.Text = "操作失败! 失败的原因是:" + ee.ToString();
}
相关文章推荐
- C#语言实现创建、删除和移动文件夹及其文件夹列表功能
- iOS激情详解之文件夹创建,移动,复制,删除以及归档和反归档
- java中File文件的创建,删除,复制,移动,以及目录文件的删除等等,主要是文件及文件夹的操作
- net8:简易的文件磁盘管理操作一(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)
- net8:简易的文件磁盘管理操作二(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)
- php文件及文件夹操作(创建、删除、移动、复制)
- C# 文件、文件夹操作语句(删除,创建,移动,检查是否存在)
- asp.net创建、删除、移动文件夹 文件
- C#程序中创建、复制、移动、删除文件或文件夹的示例
- java 的 文件、文件夹 的建立、删除、复制以及移动等功能 操作
- 文件以及文件夹处理(新建,移动,删除,复制)
- C#对文件夹的判断、创建、移动、删除
- 转:C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件
- php创建文件、php删除文件、php移动文件、php复制文件、php创建文件夹、php删除文件夹、php移动文件夹、php复制文件夹
- linux学习入门5——linux文件基本操作(linuxcast.net)(复制、移动、重命名、创建、删除文件及文件夹)
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- Python - 创建目录,切换目录,创建文件,复制文件,移动文件 ,删除文件夹及文件
- java_对文件的删除、剪切、移动和创建文件夹操作
- C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件