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);
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〉\"
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\");
- }
- }
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
【編輯推薦】
責任編輯:冰荷
來源:
chinaitlab