webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test14_8.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>请输入文件夹名称</h3>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:RadioButton ID="RadioButton1" runat="server" Text="创建文件夹" GroupName="floder" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="删除文件夹" GroupName="floder"/>
<br />
<asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" /><br />
<asp:Label ID="lblTips" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace test14_8
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = TextBox1.Text;
if (path == string.Empty) return;
int action = 0;
if (RadioButton1.Checked)
{
action = 1; // 创建
}
if (RadioButton2.Checked)
{
action = 2; //删除
}
switch (action)
{
case 1:
if (Directory.Exists(path))
{
lblTips.Text = "文件夹已存在!";
return;
}
else
{
DirectoryInfo dr = Directory.CreateDirectory(path);
lblTips.Text = "";
lblTips.Text += "创建时间:" + dr.CreationTime + "<br>";
lblTips.Text += "父文件夹:" + dr.Parent + "<br>";
}
break;
case 2:
if (Directory.Exists(path))
{
try
{
Directory.Delete(path);
lblTips.Text = "";
lblTips.Text = "文件夹已删除!";
}
catch (Exception ex)
{
lblTips.Text = ex.Message;
}
}
else
{
lblTips.Text = "文件夹不存在!";
}
break;
default:
return;
break;
}
}
}
}