Implementing ExtensionMethod For System.IO

I started programming in Clipper in the summer of 87. When they released Clipper 5.x, this was a big change at that time. The programming engines were open (like precompiled code) to programmers creating their own commands, with #xtranslate and #xcommand.

When ExtensionMethod was released in C#, see here, I do remember in Clipper, we could extend the native language with our own resources and methods.

In this post, we'll use ExtensionMethod to manipulate the files and folders, in an intuitive and fast way.

If you write WinForms applications, probably, you’ll manipulate the files. Then why create too many variables to handle fixed file names on the hard drive?

I presume that you've read the link above and know how to create an Extension Method class, so I’ll be direct and show the source code.

This code is the code that I use. I just translated the name vars from Portuguese to English.
 
ExtensionMethodIO.cs
  1. namespace System.IO  
  2. {  
  3.     public static class ExtensionMethodIO  
  4.     {  
  5.         public static bool FileExist(this string cFile) => File.Exists(cFile);  
  6.   
  7.         public static bool NotFileExist(this string cFile) => !File.Exists(cFile);  
  8.   
  9.         public static bool MkDir(this string cNewDir)  
  10.         {  
  11.             try  
  12.             {  
  13.                 if (!Directory.Exists(cNewDir))  
  14.                     Directory.CreateDirectory(cNewDir);  
  15.                 return true;  
  16.             }  
  17.             catch  
  18.             {  
  19.                 // ignored  
  20.             }  
  21.   
  22.             return false;  
  23.         }  
  24.   
  25.         public static bool CopyFile(this string source, string target)  
  26.         {  
  27.             try  
  28.             {  
  29.                 File.Copy(source, target, true);  
  30.                 var fo = new FileInfo(source);  
  31.                 var fd = new FileInfo(target);  
  32.                 if (fo.Length != fd.Length)  
  33.                     return false;  
  34.             }  
  35.             catch  
  36.             {  
  37.   
  38.                 // You can add a custom throw, or ignore  
  39.             }  
  40.   
  41.             return target.FileExist();  
  42.         }  
  43.   
  44.         public static string GetFileName(this string cFileName) => Path.GetFileName(cFileName);  
  45.         public static bool NotDirExist(this string dirName) => !Directory.Exists(dirName);  
  46.         public static bool DirExist(this string dirName) => Directory.Exists(dirName);  
  47.         public static void ShellOpen(this string file) => Diagnostics.Process.Start(file);  
  48.   
  49.     }  
  50. }  
Program.cs
  1. using System.IO;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace ConsoleApp2  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             var notepad = @"C:\Windows\System32\Notepad.exe";  
  11.   
  12.             if (notepad.NotFileExist())  
  13.             {  
  14.                 MessageBox.Show("Notepad is missing!");  
  15.                 return;  
  16.             }  
  17.   
  18.             var wd = @"C:\WorkPath";  
  19.             if (wd.MkDir())  
  20.             {  
  21.   
  22.                 if (!notepad.CopyFile($@"{wd}\{notepad.GetFileName()}"))  
  23.                 {  
  24.                     MessageBox.Show("Copy error!");  
  25.                     return;  
  26.                 }  
  27.             }  
  28.   
  29.             notepad.ShellOpen();  
  30.         }  
  31.     }  
  32. }  
Other ways to use it,
  1. if (@"C:\Windows\System32\Notepad.exe".NotFileExist())  
  2. {  
  3.   // do something  
  4. }  
Conclusion
 
With this small sample, you can expand as per your own requirement. I've more I/O implementations on production but you can do it as per your own needs.
 
Note 
The attached file contains this code working in a console app.