C#文件列表操作四大重點(diǎn)
作者:佚名
文章講解C#文件列表操作四大重點(diǎn):文件上傳、下載、刪除,以及獲得文件夾下的文件列表。
C#文件列表要點(diǎn)1:上傳文件
HTML部分:
- 〈 formid=\"form1\"runat=\"
- server\"method=\"post\"enctype=\
- "multipart/form-data\"〉
- 〈 inputid=\"FileUpLoad\"type=\"
- file\"runat=\"server\"/〉〈 br/〉
- 后臺(tái)CS部分按鈕事件
- //stringstrFileFullName=
- System.IO.Path.GetFileName(this.
- FileUpLoad.PostedFile.FileName);
- //this.FileUpLoad.PostedFile.SaveAs(Server.MapPath(
- \"./Xmlzip/\")+strFileFullName);
C#文件列表要點(diǎn)2.文件下載
- ListBox的SelectedIndexChanged事件設(shè)
- 定相關(guān)下載連接
- 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();
- }
- }
- 或者也可以通過(guò)改變Label的Text值來(lái)實(shí)現(xiàn)點(diǎn)擊
- 后實(shí)現(xiàn)文件下載的超級(jí)連接
- this.Label1.Text=\"〈 ahref=
- \\\"Xmlzip/a.rar\\\"〉a.rar〈 /a〉\"
C#文件列表要點(diǎn)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#文件列表要點(diǎn)4.得到文件夾下的文件列表
- #region得到當(dāng)前可用的文件列表
- ///〈 summary〉
- ///得到當(dāng)前可用的文件列表
- ///〈 /summary〉
- ///〈 paramname=\"IsAlert\"〉
- 是否需要彈出提示信息〈 /param〉
- privatevoidfn_getCurrFileList(boolIsAlert)
- {
- try
- {
- //查找Xmlzip文件夾下屬于其本
- 身UnitCoding的相關(guān)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(\"查無(wú)可以下載的文件!\");
- }
- }
- }
- }
- catch(Exceptionex)
- {
- ex.ToString();
- }
- }
- #endregion
【編輯推薦】
責(zé)任編輯:冰荷
來(lái)源:
chinaitlab