search
HomeBackend DevelopmentC#.Net TutorialHow to copy files and folders using asp.net
How to copy files and folders using asp.netAug 16, 2017 pm 05:05 PM
asp.netaccomplishdocument

本文主要分享了实现文件和文件夹的复制的示例代码,具有一定的参考价值,下面跟着小编一起来看下吧

话不多说,请看代码:


private void btnSave_Click(object sender, EventArgs e) //文件复制、保存方法
    {
      #region 静态复制文件(写死)
      string desPath = @"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 动态
    private void btnCopyContents_Click(object sender, EventArgs e)
    {
      string oldDir, newDir; //分别是原文件夹和目标文件夹
      FolderBrowserDialog sourceFolder = new FolderBrowserDialog();//动态生成了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 (string filepath in files) //也可以用for语句
          {
            //File.Copy(filepath,newDir+"\\"+filepath.Substring(filepath.LastIndexOf("\\")+1),true);
          //拆分了一下,更为简洁
            string nFileName ; //定义一个string类型,来获取文件名
            nFileName = filepath.Substring(filepath.LastIndexOf("\\") + 1); //获取要复制的文件夹里的文件名
            File.Copy(filepath, newDir + "\\" + nFileName, true);  //最后得出要复制的文件夹以及文件夹里的文件名并进行复制
          }
          //MessageBox.Show("复制完成");
        }
        //MessageBox.Show(sourceFolder.SelectedPath);

      }
    }
    #endregion

The above is the detailed content of How to copy files and folders using asp.net. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
wpsystem是什么文件夹wpsystem是什么文件夹Sep 01, 2022 am 11:22 AM

wpsystem文件夹是windows应用文件夹;创建WpSystem文件夹是为了存储某些特定“Microsoft Store”应用程序的数据,因此建议不要删该文件夹,因为删除之后就无法使用指定的应用。

winreagent是什么文件夹winreagent是什么文件夹Aug 26, 2022 am 11:23 AM

winreagent是在系统更新或升级的过程中创建的文件夹;该文件夹中通常包含临时文件,当更新或升级失败时,系统将通过还原先前创建的临时文件来回滚到执行更新或升级过程之前的版本。

baidunetdiskdownload是什么文件夹baidunetdiskdownload是什么文件夹Aug 30, 2022 am 10:45 AM

baidunetdiskdownload是百度网盘默认下载文件的文件夹;百度网盘是百度推出的一项云存储服务,只要下载东西到百度网盘里,都会默认保存到这个文件夹中,并且可跨终端随时随地查看和分享。

usmt.ppkg是什么文件usmt.ppkg是什么文件Sep 09, 2022 pm 02:14 PM

“usmt.ppkg”是windows自带的系统还原功能的系统备份文件;Windows系统还原是在不需要重新安装操作系统,也不会破坏数据文件的前提下使系统回到原有的工作状态,PBR恢复功能的备份文件就是“usmt.ppkg”。

mobileemumaster是什么文件mobileemumaster是什么文件Oct 26, 2022 am 11:28 AM

mobileEmuMaster是手机模拟大师的安装文件夹。手机模拟大师是PC电脑模拟运行安卓系统的免费模拟器程序,一款可以让用户在电脑上运行手机应用的软件,支持安装安卓系统中常见的apk执行文件,支持QQ、微信等生活常用应用,达到全面兼容的效果。

备份文件的扩展名通常是什么备份文件的扩展名通常是什么Sep 01, 2022 pm 03:55 PM

备份文件的扩展名通常是“.bak”;bak文件是一个备份文件,这类文件一般在'.bak前面加上应该有原来的扩展名,有的则是由原文件的后缀名和bak混合而成,在生成了某种类型的文件后,就会自动生成它的备份文件。

config是什么文件夹可以删除吗config是什么文件夹可以删除吗Sep 13, 2022 pm 03:48 PM

config是软件或者系统中的配置文件,不可以删除;该文件是在用户开机时对计算机进行初始化设置,也就是用户对系统的设置都由它来对计算机进行恢复,因此不能删除软件或者系统中的config配置文件,以免造成错误。

kml是什么文件的格式kml是什么文件的格式Sep 14, 2022 am 10:39 AM

kml是谷歌公司创建的一种地标性文件格式;该文件用于记录某一地点或连续地点的时间、经度、纬度、海拔等地理信息数据,可以被“Google Earth”和“Google Maps”识别并显示。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft