move files around with mono


/ Published in: C#
Save to your folder(s)

This is a small examble on how to move files around in mono and C#.


Copy this code and paste it in your HTML
  1. using System;
  2. using Gtk;
  3.  
  4.  
  5. public partial class MainWindow: Gtk.Window
  6. {
  7. public MainWindow (): base (Gtk.WindowType.Toplevel)
  8. {
  9. Build ();
  10. }
  11.  
  12. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  13. {
  14. Application.Quit ();
  15. a.RetVal = true;
  16. }
  17.  
  18. protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
  19. {
  20. if(filechooserbutton1.Filename == null) return;
  21. if(entry1.Text == null) return;
  22. string Src = filechooserbutton1.Filename;
  23. string Dest = entry1.Text;
  24.  
  25. System.IO.FileInfo fileInfoSrc = new System.IO.FileInfo(Src);
  26. System.IO.FileInfo fileInfoDest = new System.IO.FileInfo(Dest);
  27. if(!fileInfoSrc.Exists) return;
  28. if(fileInfoDest.Exists)
  29. {
  30. if(movebutton.Active)
  31. {
  32. moveOverwrite(Src,Dest);
  33. }
  34. else
  35. {
  36. copyOverwrite(Src,Dest);
  37. }
  38. }
  39. else
  40. {
  41. if(movebutton.Active)
  42. {
  43. System.IO.File.Move(Src,Dest);
  44. }
  45. else
  46. {
  47. System.IO.File.Copy(Src,Dest);
  48. }
  49. }
  50.  
  51. }
  52. void moveOverwrite(string Src,string Dest)
  53. {
  54. MessageDialog myMessage = new MessageDialog(this,
  55. DialogFlags.NoSeparator,
  56. MessageType.Question,
  57. ButtonsType.YesNo,
  58. "Do you really want to overwrite : " +
  59. Dest + " ?");
  60. if((ResponseType)myMessage.Run() == ResponseType.Yes)
  61. {
  62. myMessage.Destroy();
  63. System.IO.File.Move(Src,Dest);
  64. }
  65. else
  66. {myMessage.Destroy();}
  67. }
  68. void copyOverwrite(string Src , string Dest)
  69. {
  70. MessageDialog myMessage = new MessageDialog(this,
  71. DialogFlags.NoSeparator,
  72. MessageType.Question,
  73. ButtonsType.YesNo,
  74. "Do you really want to overwrite : " +
  75. Dest + " ?");
  76. if((ResponseType)myMessage.Run() == ResponseType.Yes)
  77. {
  78. myMessage.Destroy();
  79. System.IO.File.Copy(Src,Dest,true);
  80. }
  81. else
  82. {myMessage.Destroy();}
  83.  
  84. }
  85. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.