首先引入 System.IO
<%@ Import Namespace="System.IO" %>
判断目录是否存在语法
bool Directory.Exists(string path)
列出驱动器示例
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string[] drivers = Directory.GetLogicalDrives();
lb.Text += "共有 " + drivers.Length + " 个逻辑驱动器,分别是:<br>";
for (int i = 0; i < drivers.Length; i++)
{
lb.Text += Server.HtmlEncode(drivers[i]) + "<br>";
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lb" runat="server"></asp:Label>
</form>
</body>
</html>
关键是 Directory.GetLogicalDrives,注意它返回的是 string 数组。