Home Backend Development C#.Net Tutorial How to copy and delete files in C#

How to copy and delete files in C#

Oct 09, 2017 am 10:43 AM
.net method

这篇文章主要介绍了C# 复制与删除文件的实现方法的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分内容,需要的朋友可以参考下

C# 复制与删除文件的实现方法

1、首先是复制文件

首先打开我们的对话框获得文件路径,当然也可以直接编写路径


private void BtnAddFile_Click(object sender, RoutedEventArgs e) 
  { 
   OpenFileDialog openFileDialog = new OpenFileDialog(); 
   if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
   { 
    string path = openFileDialog.FileName; 
    // System.Windows.Forms.MessageBox.Show(path); 
    MyFile.addFile(path); 
   } 
  }
Copy after login

然后是复制文件的方法买这里我写的是添加,是根据我的项目来的

MyFile是一个文件操作类,OpenFileDialog需要添加引用


public static void addFile(string path)  
    { 
      string[] s = path.Split('\\'); 
 
      try 
      { 
        File.Copy(path, "E:\\Share\\"+s[s.Length-1], true);  

     //path是你要复制的文件,第二个是目标路径,注意要获得文件名      
  } 
  catch (Exception ex)  
  { 
    System.Windows.Forms.MessageBox.Show(ex.Message); 
  } 
}
Copy after login

string[] s = path.Split("\\");这是一个分割字符串的方式,我们需要获得文件名,然后拼接成目标路径,我们是不能直接复制文件到文件夹中的

2、删除


public static void deleteFile(string path) 
  { 
   if (System.IO.File.Exists(path)) 
   { 
    try 
    { 
     System.IO.File.Delete(path); 
    } 
    catch (System.IO.IOException e) 
    { 
     Console.WriteLine(e.Message); 
     return; 
    } 
   } 
  }
Copy after login

The above is the detailed content of How to copy and delete files in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to delete WeChat friends? How to delete WeChat friends How to delete WeChat friends? How to delete WeChat friends Mar 04, 2024 am 11:10 AM

How to delete WeChat friends? How to delete WeChat friends

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

How to enter bios on Colorful motherboard? Teach you two methods

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Summary of methods to obtain administrator rights in Win11 Summary of methods to obtain administrator rights in Win11 Mar 09, 2024 am 08:45 AM

Summary of methods to obtain administrator rights in Win11

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

Detailed explanation of Oracle version query method Detailed explanation of Oracle version query method Mar 07, 2024 pm 09:21 PM

Detailed explanation of Oracle version query method

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

See all articles