asp.net實現檔案和檔案夾的複製的教程

來源:互聯網
上載者:User

話不多說,請看代碼:

privatevoidbtnSave_Click(objectsender, EventArgs e)//檔案複製、儲存方法
    {
      #region 靜態複製檔案(寫死)
      stringdesPath =@"c:\1\1.chm";
      if(File.Exists(desPath))
      {
        //目標檔案已存在
        if(MessageBox.Show(("檔案已存在,是否覆蓋"),"詢問", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        == DialogResult.Yes) //選擇Yes 確定覆蓋
        {
          //複製檔案
          File.Copy(@"c:\ls\w3.chm", desPath,true);
          MessageBox.Show("覆蓋成功");
        }
      }
      else//檔案不存在
      {
        //開始複製
        File.Copy(@"c:\ls\w3.chm", desPath,true);
        MessageBox.Show("複製成功");
      }
      //顯示開啟對話方塊,傳回值為dialogResult類型,如果是OK,則使用者點擊的為開啟,否則為取消
      openFileDialog1.InitialDirectory=(@"c:\1");//選擇檔案時的預設位置
      //openfilediaglog1.filter中的fileter是過濾器的作用
      //showdialog()顯示對話方塊的方法.
      openFileDialog1.Filter ="可執行程式|*.exe|TXT文本|*.txt|圖片檔案|*.jpg|所有檔案|*.*";//可檔案類型
 
      if(openFileDialog1.ShowDialog() == DialogResult.OK)//點擊了開啟
      {
        if(saveFileDialog1.ShowDialog() == DialogResult.OK)//說明點yes 也就是確認儲存
        {
          File.Copy(openFileDialog1.FileName, saveFileDialog1.FileName,true);
          MessageBox.Show("儲存完成");
        }
      }
#endregion
    }
    //File類是對檔案操作的,包括複製、儲存、建立時間、修改時間等等等等。
    //Directory功能類似file
    #region 動態
    privatevoidbtnCopyContents_Click(objectsender, EventArgs e)
    {
      stringoldDir, newDir;//分別是原檔案夾和目標檔案夾
      FolderBrowserDialog sourceFolder =newFolderBrowserDialog();//動態產生了folderbrowserdialog這個控制項 不需要拖控制項
      sourceFolder.Description ="請選擇要複製的檔案夾";//顯示了一個簡單說明
      if(sourceFolder.ShowDialog()==DialogResult.OK)//點了確定
      {
        oldDir = sourceFolder.SelectedPath;
        sourceFolder.Description ="請選擇要複製到的檔案夾";//修改了一下sourcefolder的解說文字 便於使用者使用
        if(sourceFolder.ShowDialog()== DialogResult.OK)//如果確定 那麼執行下面代碼塊代碼
        {
          newDir = sourceFolder.SelectedPath;
          //擷取當前要複製的檔案夾中的所有檔案(注意!不包含下級檔案夾及其中的檔案)
          string[] files = Directory.GetFiles(oldDir);//定義了個字元數組來接收源檔案內需要複製的檔案
          foreach(stringfilepathinfiles)//也可以用for語句
          {
            //File.Copy(filepath,newDir+"\\"+filepath.Substring(filepath.LastIndexOf("\\")+1),true);
          //拆分了一下,更為簡潔
            stringnFileName ;//定義一個string類型,來擷取檔案名稱
            nFileName = filepath.Substring(filepath.LastIndexOf("\\") + 1);//擷取要複製的檔案夾裡的檔案名稱
            File.Copy(filepath, newDir +"\\"+ nFileName,true); //最後得出要複製的檔案夾以及檔案夾裡的檔案名稱並進行複製
          }
          //MessageBox.Show("複製完成");
        }
        //MessageBox.Show(sourceFolder.SelectedPath);
 
      }
    }
    #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.