FileInfo 和 DirectoryInfo

作者:vkvi 来源:千一网络(原创) 日期:2007-6-25

很明显,从字面意义上可以得出 FileInfo 和 DirectoryInfo 分别表示文件信息和目录信息,由于目录也是文件,所以 FileInfo 还可以表示目录的部分信息。名称空间为 System.IO。

使用都比较简单,看一下示例就明白了。

FileInfo 示例

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        //文件(夹)信息
        FileInfo fi = new FileInfo("E:\\WebPages\\test\\foo.txt");
        lb.Text += "创建时间:" + fi.CreationTime.ToString() + "<br>"; //适用于文件与文件夹
        lb.Text += "修改时间:" + fi.LastWriteTime.ToString() + "<br>"; //适用于文件与文件夹
        lb.Text += "访问时间:" + fi.LastAccessTime.ToString() + "<br>"; //适用于文件与文件夹
        lb.Text += "文件大小:" + fi.Length.ToString() + "<br>"; //只适用于文件,单位“字节”
        lb.Text += "只读:" + fi.IsReadOnly.ToString() + "<br>"; //适用于文件与文件夹
        lb.Text += "属性:" + fi.Attributes.ToString() + "<br>";
        lb.Text += "FullName: " + fi.FullName + "<br>";
        lb.Text += "Name: " + fi.Name + "<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>FileInfo</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="lb" runat="server"></asp:Label>
    </form>
</body>
</html>

DirectoryInfo 示例

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        DirectoryInfo di = new DirectoryInfo("E:\\WebPages\\test");
        lb.Text += "创建时间:" + di.CreationTime.ToString() + "<br>";
        lb.Text += "修改时间:" + di.LastWriteTime.ToString() + "<br>";
        lb.Text += "访问时间:" + di.LastAccessTime.ToString() + "<br>";
        lb.Text += "属性:" + di.Attributes.ToString() + "<br>";
        lb.Text += "FullName: " + di.FullName + "<br>";
        lb.Text += "Name: " + di.Name + "<br>";
        lb.Text += "Parent: " + di.Parent + "<br>";
        lb.Text += "Root: " + di.Root + "<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>DirectoryInfo</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="lb" runat="server"></asp:Label>
    </form>
</body>
</html>

 

相关文章
文章评论