如何利用C#操作文件和列表
这篇文章主要讲解了“如何利用C#操作文件和列表”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何利用C#操作文件和列表”吧!
阿勒泰ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
C#文件上传下载是对文件的基本操作,C#文件上传下载主要实现的是对文件的管理与应用。这里介绍的代码将实现大部分功能。
1.文件上传
如下要点:
HTML部分:
2.文件下载
ListBox的SelectedIndexChanged事件 设定相关下载连接
protected void lst_DownLoadFileList_SelectedIndexChanged(object sender, EventArgs e) { try { string strJS = \"window.open(\'Xmlzip/\"; strJS += this.lst_DownLoadFileList.SelectedItem.Text.Trim(); strJS += \"\'); return false; \"; this.imgbtn_DownLoadFile.Attributes.Add(\"onclick\", strJS); } catch (Exception ex) { ex.ToString(); } }
或者也可以通过 改变Label的Text值 来实现点击后实现文件下载的超级连接
this.Label1.Text = \"a.rar\"
3.文件删除
string strFilePath = Server.MapPath(\"../CountryFlowMgr/Xmlzip/\"+this.lst_DownLoadFileList.SelectedItem.Text.Trim()); if (File.Exists(strFilePath)) { File.Delete(strFilePath); if (File.Exists(strFilePath)) { Response.Write(\"ok\"); } else { Response.Write(\"ok\"); } }
4.得到文件夹下的文件列表
#region 得到当前可用的文件列表 ////// 得到当前可用的文件列表 /// /// 是否需要弹出提示信息 private void fn_getCurrFileList(bool IsAlert) { try { //查找Xmlzip文件夹下 属于其本身UnitCoding的相关zip文件 string strXmlZipDirectory = Server.MapPath(\"../Xmlzip/\"); if (Directory.Exists(strXmlZipDirectory)) { //DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory); DirectoryInfo di = new DirectoryInfo(strXmlZipDirectory); FileInfo[] FI = di.GetFiles(\"*.zip\");//只查.zip文件 if (FI.Length > 0) { lst_DownLoadFileList.Items.Clear(); foreach (FileInfo tmpFI in FI) { ListItem tmpItem = new ListItem(); tmpItem.Text = tmpFI.Name; lst_DownLoadFileList.Items.Add(tmpItem); } lst_DownLoadFileList.SelectedIndex = 0; } else { if (IsAlert) { Response.write(\"查无可以下载的文件!\"); } } } } catch (Exception ex) { ex.ToString(); } } #endregion
感谢各位的阅读,以上就是“如何利用C#操作文件和列表”的内容了,经过本文的学习后,相信大家对如何利用C#操作文件和列表这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
本文题目:如何利用C#操作文件和列表
网站链接:http://scjiangan.com/article/pegggp.html