网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 ASP.NET创建、删除和移动文件夹及文件夹列表功能
内容
    本文采用C#语言实现创建,删除和移动文件夹以及文件夹列表的功能,代码如下:
    使用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>
    <tr>
    <td colspan="2">
    文件夹中文件列表:
    </td>
    </tr>
    <tr>
    <td>
    请输入要删除文件的路径:
    </td>
    <td>
    <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">
    <asp:ListBox ID="FileListBox" runat="server" Height="192px" Width="184px"></asp:ListBox>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:Button ID="ExistButton" runat="server" Text="确定" OnClick="ExistButton_Click" />
    </td>
    </tr>
    </table>
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/18 14:46:26