How to copy files and folders using asp.net
本文主要分享了实现文件和文件夹的复制的示例代码,具有一定的参考价值,下面跟着小编一起来看下吧
话不多说,请看代码:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

In Linux systems, you can use the following command to view the contents of the log file: tail command: The tail command is used to display the content at the end of the log file. It is a common command to view the latest log information. tail [option] [file name] Commonly used options include: -n: Specify the number of lines to be displayed, the default is 10 lines. -f: Monitor the file content in real time and automatically display the new content when the file is updated. Example: tail-n20logfile.txt#Display the last 20 lines of the logfile.txt file tail-flogfile.txt#Monitor the updated content of the logfile.txt file in real time head command: The head command is used to display the beginning of the log file

Detailed explanation of the role of .ibd files in MySQL and related precautions MySQL is a popular relational database management system, and the data in the database is stored in different files. Among them, the .ibd file is a data file in the InnoDB storage engine, used to store data and indexes in tables. This article will provide a detailed analysis of the role of the .ibd file in MySQL and provide relevant code examples to help readers better understand. 1. The role of .ibd files: storing data: .ibd files are InnoDB storage
