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

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

HTML部分:

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

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

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

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

 
 
 
  1. stringstrFilePath=Server.MapPath(  
  2. \"../CountryFlowMgr/Xmlzip/\"+this.lst_  
  3. DownLoadFileList.SelectedItem.Text.Trim());  
  4. if(File.Exists(strFilePath))  
  5. {  
  6. File.Delete(strFilePath);  
  7. if(File.Exists(strFilePath))  
  8. {  
  9. Response.Write(\"ok\");  
  10. }  
  11. else 
  12. {  
  13. Response.Write(\"ok\");  
  14. }  

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

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

【编辑推荐】

  1. C#中定义装箱和拆箱详解
  2. 浅谈C#类型系统
  3. 三种不同的C#异常类型
  4. 详细介绍C#编译器
  5. C#异常机制的相关解释
THE END