Ways to implement copying and deleting files in C #

Source: Internet
Author: User
This article mainly introduces the C # copy and delete file implementation method of the relevant information, hope that through this article can help everyone, so that you understand this part of the content, the need for friends can refer to the next

How to implement C # copy and delete files

1, the first is to copy files

First open our dialog box to get the file path, of course, you can also write the path directly


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);    }   }

And then the method of copying the file to buy here I write is added, is based on my project

MyFile is a file operation class, OpenFileDialog need to add a reference


public static void AddFile (string path)      {       string[] s = path. Split (' \ \ ');        Try       {         file.copy (path, "e:\\share\\" +s[s.length-1], true);       Path is the file you want to copy, the second is the destination path, note to get the file name        }   catch (Exception ex)    {     System.Windows.Forms.MessageBox.Show ( Ex. Message);   } }

string[] s = path. Split ("\ \"), this is a way to split the string, we need to get the file name, and then stitching it into the target path, we cannot directly copy the file into the folder

2. Delete


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;}}}   
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.