ASP.NET Tutorial/File Directory/DriveInfo

Материал из .Net Framework эксперт
Версия от 11:57, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Displaying local drive information (C#)

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        System.IO.DriveInfo drive = new System.IO.DriveInfo(@"C:\");
        lblDriveName.Text = drive.Name;
        lblDriveType.Text = drive.DriveType.ToString();
        lblAvailableFreeSpace.Text = drive.AvailableFreeSpace.ToString();
        lblDriveFormat.Text = drive.DriveFormat;
        lblTotalFreeSpace.Text = drive.TotalFreeSpace.ToString();
        lblTotalSize.Text = drive.TotalSize.ToString();
        lblVolumeLabel.Text = drive.VolumeLabel;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr><td>Drive Name:</td><td>
                <asp:Label ID="lblDriveName" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Drive Type:</td><td>
                <asp:Label ID="lblDriveType" runat="server" Text="Label"/>
            </td></tr>
            <tr><td>Available Free Space:</td><td>
               <asp:Label ID="lblAvailableFreeSpace" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Drive Format:</td><td>
                <asp:Label ID="lblDriveFormat" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Total Free Space:</td><td>
                <asp:Label ID="lblTotalFreeSpace" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Total Size:</td><td>
                <asp:Label ID="lblTotalSize" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Volume Label</td><td>
                <asp:Label ID="lblVolumeLabel" runat="server" Text="Label" /> 
            </td></tr>
        </table>
    
    </div>
    </form>
</body>
</html>


Displaying local drive information (VB)

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim drive As New System.IO.DriveInfo("C:\")
        lblDriveName.Text = drive.Name
        lblDriveType.Text = drive.DriveType.ToString()
        lblAvailableFreeSpace.Text = drive.AvailableFreeSpace.ToString()
        lblDriveFormat.Text = drive.DriveFormat
        lblTotalFreeSpace.Text = drive.TotalFreeSpace.ToString()
        lblTotalSize.Text = drive.TotalSize.ToString()
        lblVolumeLabel.Text = drive.VolumeLabel
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr><td>Drive Name:</td><td>
                <asp:Label ID="lblDriveName" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Drive Type:</td><td>
                <asp:Label ID="lblDriveType" runat="server" Text="Label"/>
            </td></tr>
            <tr><td>Available Free Space:</td><td>
               <asp:Label ID="lblAvailableFreeSpace" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Drive Format:</td><td>
                <asp:Label ID="lblDriveFormat" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Total Free Space:</td><td>
                <asp:Label ID="lblTotalFreeSpace" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Total Size:</td><td>
                <asp:Label ID="lblTotalSize" runat="server" Text="Label" />
            </td></tr>
            <tr><td>Volume Label</td><td>
                <asp:Label ID="lblVolumeLabel" runat="server" Text="Label" /> 
            </td></tr>
        </table>
    
    </div>
    </form>
</body>
</html>


Enumerating file system directories (C#)

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
        {
            TreeNode node = new TreeNode();
            node.Value = drive.Name;
            if (drive.IsReady)
            {
                node.Text = drive.Name + " - (free space: " + drive.AvailableFreeSpace + ")";
                LoadDirectories(node, drive.Name);
            }
            else
                node.Text = drive.Name + " - (not ready)";
            this.TreeView1.Nodes.Add(node);
        }
    }
    private void LoadDirectories(TreeNode parent, string path)
    {
        System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(path);
        try
        {
            foreach (System.IO.DirectoryInfo d in directory.GetDirectories())
            {
                TreeNode node = new TreeNode(d.Name, d.FullName);
                parent.ChildNodes.Add(node);
                LoadDirectories(node, d.FullName);
            }
        }
        catch (System.UnauthorizedAccessException e)
        {
            parent.Text += " (Access Denied)";
        }
        catch (System.IO.IOException e)
        {
            parent.Text += " (Unknown Error: " + e.Message + ")";
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:TreeView ID="TreeView1" runat="server"></asp:TreeView>
                </td>
            </tr>
        </table>    
    </div>
    </form>
</body>
</html>


Enumerating file system directories (VB)

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each drive As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives()
            Dim node As TreeNode = New TreeNode()
            node.Value = drive.Name
            If (drive.IsReady) Then
                node.Text = drive.Name & " - (free space: " & drive.AvailableFreeSpace & ")"
                LoadDirectories(node, drive.Name)
            Else
                node.Text = drive.Name & " - (not ready)"
            End If
            Me.TreeView1.Nodes.Add(node)
        Next        
    End Sub
    Private Sub LoadDirectories(ByVal parent As TreeNode, ByVal path As String)
        
        Dim directory As System.IO.DirectoryInfo = _
                  New System.IO.DirectoryInfo(path)
        
        Try
            For Each d As System.IO.DirectoryInfo In directory.GetDirectories()
            
                Dim node As TreeNode = New TreeNode(d.Name, d.FullName)
                parent.ChildNodes.Add(node)
                "Recurse the current directory
                LoadDirectories(node, d.FullName)
            Next
        Catch ex As System.UnauthorizedAccessException
            parent.Text += " (Access Denied)"
        Catch ex As System.IO.IOException
            parent.Text += " (Unknown Error: " + ex.Message + ")"
        End Try
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td valign="top">
                    <asp:TreeView ID="TreeView1" runat="server"></asp:TreeView>
                </td>
            </tr>
        </table>    
    </div>
    </form>
</body>
</html>


Enumerating through local file system drives (C#)

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
        {
            TreeNode node = new TreeNode();
            node.Value = drive.Name;
            if (drive.IsReady)
            {
                node.Text = drive.Name + " - (free space: " + drive.AvailableFreeSpace + ")";
            }
            else
                node.Text = drive.Name + " - (not ready)";
            this.TreeView1.Nodes.Add(node);
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td valign="top">
                    <asp:TreeView ID="TreeView1" runat="server"></asp:TreeView>
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


Enumerating through local file system drives (VB)

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each drive As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives()
            Dim node As TreeNode = New TreeNode()
            node.Value = drive.Name
            If (drive.IsReady) Then
                node.Text = drive.Name & _
                            " - (free space: " & drive.AvailableFreeSpace & ")"
            Else
                node.Text = drive.Name & " - (not ready)"
            End If
            Me.TreeView1.Nodes.Add(node)
        Next        
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td style="width: 100px" valign="top">
                    <asp:TreeView ID="TreeView1" runat="server"></asp:TreeView>
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>