C# 檔案操作(上傳 下載 刪除 檔案清單…)

來源:互聯網
上載者:User

using System.IO;

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
更多更詳細的以後再做補充

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.