asp.net相关的一些代码
显示目录下的内容
using System.IO;
DirectoryInfo di = new DirectoryInfo(Server.MapPath("Views/video")); FileSystemInfo[] dis = di.GetFileSystemInfos(); for (int i = 0; i < dis.Length; i++) { Response.Write(dis[i].Name+dis[i].CreationTime+"<p>"); }
得到CheckBoxList中选中的值
public string getcheck(CheckBoxList checkList, string separator) { string selval = ""; for (int i = 0; i < checkList.Items.Count; i++) { if (checkList.Items[i].Selected) { selval += checkList.Items[i].Text + separator; } } if (selval.Length > 1) { selval = selval.Substring(0, selval.Length - 1); } return selval; }
asp.net分页
html
<div class="textdiv"> 当前面页为【<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>】 总页数据【<asp:Label ID="labBackPage" runat="server" Text="10"></asp:Label>】 <asp:linkbutton ID="lnkbtnOne" runat="server" Text="第一页" OnClick="lnkbtnOne_Click"></asp:linkbutton> <asp:linkbutton ID="lnkbtnUp" runat="server" Text="上一页" OnClick="inkbtnUp_Click"></asp:linkbutton> <asp:linkbutton ID="lnkbtnNext" runat="server" Text="下一页" OnClick="lnkbtnNext_Click"></asp:linkbutton> <asp:linkbutton ID="lnkbtnBack" runat="server" Text="最底页" OnClick="lnkbtnBack_Click"></asp:linkbutton> </div>
cs:
protected void Page_Load(object sender, EventArgs e) { dlBind(); } protected void dlBind() { int curpage = Convert.ToInt32(this.labPage.Text); PagedDataSource ps = new PagedDataSource(); //数据库连接数据,并打开 OleDbConnection oCon = new OleDbConnection(ConfigurationManager.AppSettings["accessCon"]); oCon.Open(); string maintenance_sql = "select a.ID,c.shop_name,b.branch_name,a.maintenance_time,a.maintenance_content,a.maintenance_state,a.shop_id from maintenance a,branch b,shop_name c where a.shop_id=c.id and c.shop_branch=b.branch_id ORDER BY a.maintenance_time"; //执行查询得到用于匹配数据集的数据 OleDbDataAdapter maintenance_date = new OleDbDataAdapter(maintenance_sql, oCon); //声明内存中的一个表 DataSet myda = new DataSet(); //将数据存于内存表中 maintenance_date.Fill(myda, "maintenance"); ps.DataSource = myda.Tables["maintenance"].DefaultView; ps.AllowPaging = true; ps.PageSize = 20; ps.CurrentPageIndex = curpage -1; this.lnkbtnBack.Enabled = true; this.lnkbtnNext.Enabled = true; this.lnkbtnOne.Enabled = true; this.lnkbtnUp.Enabled = true; if (curpage == 1) { this.lnkbtnOne.Enabled = false; this.lnkbtnUp.Enabled = false; } if (curpage == ps.PageCount) { this.lnkbtnBack.Enabled = false; this.lnkbtnNext.Enabled = false; } this.labBackPage.Text = Convert.ToString(ps.PageCount); //指定repeater的数据源为myda this.maintenance_repeater1.DataSource = ps; //匹配repeater中的子控件绑定,aspx页面上用<%# DataBinder.Eval(Container.DataItem, "maintenance_time")%>显示相关字段 this.maintenance_repeater1.DataBind(); oCon.Close(); } protected void lnkbtnOne_Click(object sender, EventArgs e) { this.labPage.Text = "1"; dlBind(); } protected void inkbtnUp_Click(object sender, EventArgs e) { this.labPage.Text = Convert.ToString( Convert.ToInt32(this.labPage.Text) - 1); dlBind(); } protected void lnkbtnNext_Click(object sender, EventArgs e) { this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) +1 ); dlBind(); } protected void lnkbtnBack_Click(object sender, EventArgs e) { this.labPage.Text = this.labBackPage.Text; dlBind(); }
检文件的MD5
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(GetMD5HashFromFile("d:\\Hash_1.0.4.exe"));
}
public static string GetMD5HashFromFile(string filePath) { try { FileStream file = new FileStream(filePath, FileMode.Open); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString().ToUpper(); } catch (Exception ex) { throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message); } }
删除目录下的文件
private void button1_Click(object sender, EventArgs e) { string path = "d:\\bb"; if (Directory.Exists(path)) Directory.Delete(path, true); Directory.CreateDirectory(path); }