work070
webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work070.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> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="DriverName" HeaderText="分區名稱"/> <asp:BoundField DataField="DriverType" HeaderText="分區類型"/> <asp:BoundField DataField="DriverFormat" HeaderText="文件系統"/> <asp:BoundField DataField="VolumeLabel" HeaderText="卷標"/> <asp:BoundField DataField="TotalSize" HeaderText="總容量"/> <asp:BoundField DataField="AvailableFreeSpace" HeaderText="可用容量"/> <asp:BoundField DataField="TotalFreeSpace" HeaderText="空閒容量"/> <asp:BoundField DataField="Percent" HeaderText="剩餘百分比" DataFormatString="{0}%"/> </Columns> </asp:GridView> </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;
namespace work070{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e){ if (!Page.IsPostBack) { System.IO.DriveInfo[] drivers = System.IO.DriveInfo.GetDrives();
System.Data.DataTable table1 = new System.Data.DataTable(); table1.Columns.Add("DriverName",typeof(string)); table1.Columns.Add("DriverType", typeof(string)); table1.Columns.Add("DriverFormat", typeof(string)); table1.Columns.Add("VolumeLabel", typeof(string)); table1.Columns.Add("AvailableFreeSpace",typeof(long)); table1.Columns.Add("TotalFreeSpace", typeof(long)); table1.Columns.Add("TotalSize", typeof(long)); table1.Columns.Add("Percent", typeof(float));
foreach (System.IO.DriveInfo info in drivers) { System.Data.DataRow row1 = table1.NewRow(); row1["DriverName"] = info.Name; row1["DriverType"] = info.DriveType; row1["DriverFormat"] = info.DriveFormat; row1["VolumeLabel"] = info.VolumeLabel; row1["AvailableFreeSpace"] = info.AvailableFreeSpace; row1["TotalFreeSpace"] = info.TotalFreeSpace; row1["TotalSize"] = info.TotalSize; row1["Percent"] = (info.TotalFreeSpace * 100) / info.TotalSize; table1.Rows.Add(row1); }
GridView1.DataSource = table1; GridView1.DataBind(); } } }}work071
webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work071.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>DirectoryInfo示例</title></head><body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Name" HeaderText="名稱" /> <asp:BoundField DataField="FullName" HeaderText="全名" /> <asp:BoundField DataField="CreateTime" HeaderText="創建時間" /> <asp:BoundField DataField="ReadOnly" HeaderText="只讀" /> <asp:BoundField DataField="IsDirectory" HeaderText="是否目錄" /> </Columns> </asp:GridView> </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;
namespace work071{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e){ if (!Page.IsPostBack) { System.Data.DataTable table1 = new System.Data.DataTable(); table1.Columns.Add("Name",typeof(string)); table1.Columns.Add("FullName",typeof(string)); table1.Columns.Add("CreateTime",typeof(DateTime)); table1.Columns.Add("ReadOnly",typeof(bool)); table1.Columns.Add("IsDirectory",typeof(string)); string path = Server.MapPath("~"); System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(path); System.IO.FileSystemInfo[] fileList = info.GetFileSystemInfos("**"); System.Data.DataRow row = null;
foreach (System.IO.FileSystemInfo fsi in fileList) { row = table1.NewRow();
row["Name"] = fsi.Name; row["FullName"] = fsi.FullName; row["CreateTime"] = fsi.CreationTime; row["ReadOnly"] = (fsi.Attributes & System.IO.FileAttributes.ReadOnly) == 0 ? false : true; row["IsDirectory"] = (fsi.Attributes & System.IO.FileAttributes.Directory) == 0 ? "文件" : "目錄"; table1.Rows.Add(row); }
GridView1.DataSource = table1; GridView1.DataBind(); } } }}work072
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work072.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>FileInfo類的示例</title></head><body> <form id="form1" runat="server"> <div> <table border="0"> <tr> <td> <asp:Button ID="btnCreateFile" runat="server" Text="創建文件" OnClick="btnCreateFile_Click" /> </td> <td> <asp:Button ID="btnWriteFile" runat="server" Text="寫入內容" OnClick="btnWriteFile_Click" /> </td> <td> <asp:Button ID="btnDeleteFile" runat="server" Text="刪除文件" OnClick="btnDeleteFile_Click" /> </td> </tr> </table> <table border="1"> <tr> <td>文件名</td> <td>創建時間</td> <td>文件大小</td> <td>全路徑</td> </tr> <% ListAllFile(); %> </table> </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;
namespace work072{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e){
}
protected void btnCreateFile_Click(object sender, EventArgs e){ string path = Server.MapPath("~"); for (int i = 0; i < 10; i++) { System.IO.FileStream stream = System.IO.File.Create(path + (i +1) + ".txt"); stream.Close(); }
}
protected void btnWriteFile_Click(object sender, EventArgs e){ string path = Server.MapPath("~"); System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path); System.IO.FileInfo[] files = dir.GetFiles("*.txt"); System.IO.StreamWriter writer = null;
foreach (System.IO.FileInfo f in files) { writer = f.AppendText(); writer.WriteLine("蝦米大王教你學編程系列之ASP.NET中級教程"); writer.Close(); } }
protected void btnDeleteFile_Click(object sender, EventArgs e){ string path = Server.MapPath("~"); System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path); System.IO.FileInfo[] files = dir.GetFiles("*.txt"); System.IO.StreamWriter writer = null;
foreach (System.IO.FileInfo f in files) { f.Delete(); } }
protected void ListAllFile(){ string path = Server.MapPath("~"); System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
System.IO.FileInfo[] files = dir.GetFiles("*.txt"); foreach (System.IO.FileInfo f in files) { Response.Write("<tr>"); Response.Write(string.Format("<td>{0}</td>",f.Name)); Response.Write(string.Format("<td>{0}</td>", f.CreationTime)); Response.Write(string.Format("<td>{0} 字節</td>", f.Length)); Response.Write(string.Format("<td>{0}</td>", f.FullName)); Response.Write("</tr>"); } } }}
有你的支持,我會更有動力。