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