溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何利用C#操作文件和列表

發布時間:2021-06-16 14:37:53 來源:億速云 閱讀:186 作者:chen 欄目:編程語言

這篇文章主要講解了“如何利用C#操作文件和列表”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何利用C#操作文件和列表”吧!

C#文件上傳下載是對文件的基本操作,C#文件上傳下載主要實現的是對文件的管理與應用。這里介紹的代碼將實現大部分功能。

1.文件上傳

如下要點:

HTML部分:

<form id=\"form1\" runat=\"server\" method=\"post\" enctype=\"multipart/form-data\">     <input id=\"FileUpLoad\" type=\"file\" runat=\"server\"/><br />     后臺CS部分 按鈕事件      //string strFileFullName = System.IO.Path.GetFileName(this.FileUpLoad.PostedFile.FileName);      //this.FileUpLoad.PostedFile.SaveAs(Server.MapPath(\"./Xmlzip/\") + strFileFullName);

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 href=\\\"Xmlzip/a.rar\\\">a.rar</a>\"

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 得到當前可用的文件列表          /// <summary>          /// 得到當前可用的文件列表          /// </summary>          /// <param name=\"IsAlert\">是否需要彈出提示信息</param>          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#操作文件和列表這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女