asp.net中級教程-40

2022-01-09 碼農新勢力

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>"); } } }}

有你的支持,我會更有動力。

相關焦點

  • ASP.Net安裝簡明手冊
    其他網站找到的安裝資料(一)終於空下一段時間,可以學一些新東西,看了一下有關asp+的相關資料,覺得很值得學一下,所以就一邊學習一邊寫下這個教程,對於自己來說,可以作為學習筆記,對於別人,尤其是那些E文不太好的朋友可以作為一個可看的中文資料吧,起個拋磚引玉的作用。由於水平所限,錯誤在所難免,希望大家能批評指正。
  • 自學MVC看這裡——全網最全ASP.NET MVC 教程匯總
    - Part.2ASP.NET MVC中使用View Model分離領域模型探秘ASP.NET MVC框架傳遞加載過程3.How: 如何使用Asp.net MVC 框架進行開發, Asp.net MVC 入門教程及實例開發七天學會ASP.NET MVC 5系列教程,該系列入門教程由淺至深,介紹了MVC5的使用,涉及了一些安全方面的功能(授權認證,角色管理,
  • ASP.NET Core學習資源匯總
    這本手冊內容是一個入門的教程。(可以使用autofac或者其他來實現屬性注入)https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.htmlASP.NET Core 中依賴注入的N種玩法https://www.cnblogs.com/artech/p/di-asp-net-core-pipeline-2.
  • 如何實現Asp與Asp.Net共享Session
    > 語言 > 關鍵詞 > 最新資訊 > 正文 如何實現Asp與Asp.Net共享Session 在.net
  • 簡明 ASP.NET Core 手冊
    編者:在4月份推送過這篇文章 簡明 ASP.NET Core 手冊 ,今天再次推薦這篇文章,是因為原作者更新到了新版本1.1.0,改動很大,幾乎所有章節都有很大程度的調整,這些調整都是根據讀者的建議而做,而且對於很多.NET Core的初學者來說,這是一個非常好的教程
  • asp.net連接MySQL,在GridView上實現增刪改查:前臺配置參數實現
    asp.net中如果想要在GridView控制項上實現增刪改查,通過給GridView添加數據源就可以實現,前提是使用的資料庫必須是數據源中有的。正好最近做的asp.net項目就需要用在GridView中使用mysql連接,所以在網上找了很多教程,都比較零散,用的最多的就是在底層.cs文件中通過GridView的一些事件去做相應的處理,而且很麻煩,我花了好幾個小時終於找到了在前臺通過配置的方式實現增刪改查,下面是具體步驟。一、在頁面上添加一個GridView控制項和一個SqlDataSource控制項。
  • asp.net core 5.0 中的 JsonConsole
    asp.net core 5.0 中的 JsonConsoleIntroasp.net core 5.0 中日誌新增了 JsonConsole,還是輸出日誌到 Console,但是會應用 Json 格式的一個 Formatter 把日誌格式化成 json 再輸出到控制臺
  • 目前中國網站服務端開發主要有PHP、asp.net、Java三種語言
    通過各招聘網站招聘程式設計師的類型統計得出結果是目前中國網站服務端開發主要有PHP、asp.net、Java三種語言,其他佔少數有潛力有C#,C++等,而Python也開始被開發者使用在服務端開發。從招聘的數據信息顯示,PHP編程開發主要集中在中小網站,個人站點,個人博客等開發,而政府網站更喜歡asp.net開發,Java則主要在電商,支付系統,安全數據的等方面有優勢。
  • NET開發-ASP.NET WebForm應用程式中,使用C#操作TextBox的屬性
    ID屬性ID屬性是每個asp.net每個伺服器控制項必須具備的屬性,用來指定控制項的唯一ID值。TextBox控制項的ID值最好以「txt+有意義的單詞」組成,便於後臺C#代碼中識別並賦值和獲取值。1.2.2. Text屬性TextBox控制項的Text屬性用於給文本框賦值或取值,以及獲取用戶輸入的值。
  • 「謝燦asp.net三層架構」3、創建增刪改查及複雜操作的存儲過程
    《謝燦asp.net三層架構》系列教程由小燦燦IT首發百度平臺,希望對各位喜歡計算機的同學有所幫助!關注+分享+評論+點讚,是對我們最好的支持!有了您的支持,我們堅信我們會做得更好!「謝燦asp.net三層架構」2、創建資料庫的基本步驟及其注意事項
  • NET開發-在ASP.NET WebForm應用程式中使用C#操作Label控制項的屬性
    屬性拖放好的Label控制項完整的源碼為:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>1.2.1. ID屬性ID屬性是必須屬性,指定Label控制項的唯一ID值,最好修改以「lbl+有意義的單詞」為ID值,便於後臺代碼識別。
  • ASP.NET Core - Razor頁面之Handlers處理方法
    <input type="submit" value="Save First" asp-page-handler="First" class="btn btn-primary btn-xs" />    </form>    <form method="POST">        <div>Description: <input asp-for=
  • ASP.NET Core MVC四種枚舉綁定方式
    所以到此我們研究結束,此方法應該是被.net core mvc團隊已經棄用,我們繼續往下看。雖然上述是.net core提供給我們最好的方案,確實很好,但是我們實際要的效果不是這樣,我們來舉一個實際場景,比如如下枚舉類。
  • ASP.NET Web 開發課程的教與學
    >授課教師可以免費申請樣書課程名稱:ASP.NET Web應用開發技術 英文名稱:ASP.NET Web Development Technologies課程總學時:64     講課:40 (3)http://www.asp.net/.
  • 在Ubuntu下搭建ASP.NET 5開發環境
    不過這個不是本文的重點,而且很容易就能搜到大量教程,具體就不說了。我參考的這篇文章:http://www.linuxidc.com/Linux/2012-05/59663.htm0x02 安裝ASP.NET 5開發環境下面重點開始了,主要步驟參照的微軟的官方文檔:https://docs.asp.net/en/latest/getting-started/installing-on-linux.html
  • asp.net mvc 自定義 pager 封裝與優化
    asp.net
  • ASP.NET學習篇(3)——幾個簡單的ASP.ENT的例子
    該函數中的Message、Name、Category並沒有定義,它們來自下面的代碼: 以下為引用的內容:    <form action="intro6.aspx" method="post" runat="server">       <h3> Name: <asp:textbox id
  • asp.net core 使用 TestServer 來做集成測試
    asp.net core 使用 TestServer 來做集成測試Intro之前我的項目裡的集成測試是隨機一個埠,每次都真實的啟動一個 WebServer,之前也有看到過微軟文檔上 TestServer 的介紹,當時沒仔細看過以為差不多就沒用,一直是啟動了一個真正的
  • ASP.NET定製簡單的錯誤處理頁面
    首頁 > 語言 > 關鍵詞 > asp最新資訊 > 正文 ASP.NET定製簡單的錯誤處理頁面
  • 請求如何進入ASP.NET MVC框架
    熟悉asp.net的朋友都知道,asp.net請求實際都是交給HttpHandler處理(實現了IHttpHandler的類型)。無論是.aspx,.ashx,.asmx 還是MVC裡的Action,請求都會交給HttpHandler。具體是在管道事件中,會根據請求創建一個HttpHandler,並執行它的PR方法。