C#文件列表操作四大重点

开发 后端
文章讲解C#文件列表操作四大重点:文件上传、下载、删除,以及获得文件夹下的文件列表。

C#文件列表要点1:上传文件

HTML部分:

〈 formid=\"form1\"runat=\" 
server\"method=\"post\"enctype=\  
"multipart/form-data\"〉  
〈 inputid=\"FileUpLoad\"type=\" 
file\"runat=\"server\"/〉〈 br/〉  
后台CS部分按钮事件  
//stringstrFileFullName=  
System.IO.Path.GetFileName(this.  
FileUpLoad.PostedFile.FileName);  
//this.FileUpLoad.PostedFile.SaveAs(Server.MapPath(  
\"./Xmlzip/\")+strFileFullName); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

C#文件列表要点2.文件下载

ListBox的SelectedIndexChanged事件设  
定相关下载连接  
protectedvoidlst_DownLoadFileList  
_SelectedIndexChanged(objectsender,EventArgse)  
{  
try  
{  
stringstrJS=\"window.open(\'Xmlzip/\";  
strJS+=this.lst_DownLoadFileList.  
SelectedItem.Text.Trim();  
strJS+=\"\');returnfalse;\";  
this.imgbtn_DownLoadFile.Attributes.  
Add(\"onclick\",strJS);  
}  
catch(Exceptionex)  
{  
ex.ToString();  
}  
}  
或者也可以通过改变Label的Text值来实现点击  
后实现文件下载的超级连接  
this.Label1.Text=\"〈 ahref=  
\\\"Xmlzip/a.rar\\\"〉a.rar〈 /a〉\" 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

C#文件列表要点3.文件删除

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

C#文件列表要点4.得到文件夹下的文件列表

#region得到当前可用的文件列表  
///〈 summary〉  
///得到当前可用的文件列表  
///〈 /summary〉  
///〈 paramname=\"IsAlert\"〉  
是否需要弹出提示信息〈 /param〉  
privatevoidfn_getCurrFileList(boolIsAlert)  
{  
try  
{  
//查找Xmlzip文件夹下属于其本  
身UnitCoding的相关zip文件  
stringstrXmlZipDirectory=  
Server.MapPath(\"../Xmlzip/\");  
if(Directory.Exists(strXmlZipDirectory))  
{  
//DirectoryInfodi=newDirectoryInfo(  
Environment.CurrentDirectory);  
DirectoryInfodi=newDirectoryInfo(  
strXmlZipDirectory);  
 
FileInfo[]FI=di.GetFiles(\"*.zip\" 
);//只查.zip文件  
if(FI.Length〉0)  
{  
lst_DownLoadFileList.Items.Clear();  
foreach(FileInfotmpFIinFI)  
{  
ListItemtmpItem=newListItem();  
tmpItem.Text=tmpFI.Name;  
lst_DownLoadFileList.Items.Add(tmpItem);  
}  
lst_DownLoadFileList.SelectedIndex=0;  
}  
else 
{  
if(IsAlert)  
{  
Response.write(\"查无可以下载的文件!\");  
}  
}  
}  
}  
catch(Exceptionex)  
{  
ex.ToString();  
}  
}  
#endregion  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.

【编辑推荐】

  1. C#中定义装箱和拆箱详解
  2. 浅谈C#类型系统
  3. 三种不同的C#异常类型
  4. 详细介绍C#编译器
  5. C#异常机制的相关解释
责任编辑:冰荷 来源: chinaitlab
相关推荐

2009-08-21 13:18:25

C#异步调用

2024-03-11 14:48:34

2010-05-25 08:34:10

C# 4.0

2015-07-17 09:50:16

Carthage优劣比较

2018-11-27 14:04:16

企业上云互联网云平台

2010-07-14 14:46:57

Perl操作符

2011-03-21 09:01:49

CSS框架

2010-05-17 10:20:44

Linux备份

2009-09-01 10:28:38

C#追加文件

2009-09-01 09:37:15

C#写文件

2009-05-05 10:53:18

软件博览会

2016-03-30 11:51:55

2010-10-22 13:22:04

微软云计算

2013-01-06 10:44:43

微软Windows 8云计算

2013-05-20 08:56:13

2009-09-01 10:10:51

C# StreamRe

2009-08-31 18:38:59

C#写文件

2009-08-18 17:05:08

C#操作xml文件

2011-04-21 09:14:47

iOS 4.3Android 3.0webOS 3.0
点赞
收藏

51CTO技术栈公众号