Advertisement
heinrich23

gscopy.exe

Sep 18th, 2024 (edited)
2,970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | Source Code | 0 0
  1. using System.Reflection;
  2. using System.IO;
  3. using Microsoft.Win32.Api;
  4. using System.Runtime.Intrinsics.X86;
  5.  
  6. namespace gscopy
  7. {
  8.     /// <summary>
  9.     /// gscopy simple console program, that demonstrates
  10.     /// File.Copy, File.Move, Win32 Api kernel32.dll File.XCopy or File.Write.
  11.     /// <see cref="NativeWrapper.XCopy(string, string)">xcopy</see> switch uses
  12.     /// <seealso href="https://github.com/heinrichelsigan/cmdcopy/blob/master/gscopy/InternalWrapper.cs">
  13.     /// Win32.NativeApi.InternalWrapper</seealso> api.
  14.     /// </summary>
  15.     public class Program
  16.     {
  17.         internal static string ProgName { get => Path.GetFileName(Assembly.GetExecutingAssembly().Location); }
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             int blckSze = -1;
  22.             string input = "", output = "", mode = "copy";
  23.  
  24.             if (args.Length < 2)
  25.                 Usage();
  26.  
  27.             input = args[0];
  28.             output = args[1];
  29.  
  30.             if (args.Length >= 3)
  31.                 mode = args[2].ToLower();
  32.  
  33.             if (args.Length >= 4)
  34.                 if (!Int32.TryParse(args[3], out blckSze))
  35.                     blckSze = -1; // Cannot parse
  36.  
  37.             if (!File.Exists(input))
  38.                 Usage($"Input file {input} doesn't exist, can't copy!", 1);
  39.  
  40.             if (input.Equals(output, StringComparison.InvariantCultureIgnoreCase))
  41.                 Usage($"Input file {input} equals output {output}", 2);
  42.  
  43.             switch (mode)
  44.             {
  45.                 case "move":    File.Move(input, output, true); break;
  46.                 case "write":   GsWrite(input, output, blckSze); break;
  47.                 case "xcopy":   NativeWrapper.XCopy(input, output); break;
  48.                 case "copy":
  49.                 default:        File.Copy(input, output, true); break;
  50.             }
  51.         }
  52.  
  53.         internal static void Usage(string msg = "", int exitCode = 0)
  54.         {
  55.             if (!string.IsNullOrEmpty(msg))
  56.                 Console.Error.WriteLine(msg);
  57.             Environment.ExitCode = exitCode;
  58.             Console.Out.Write($"Usage: {ProgName} source destination [copy|write|move] blocksize\r\n");
  59.             Environment.Exit(Environment.ExitCode);
  60.         }
  61.  
  62.         internal static void GsWrite(string inFile, string outFile, int bs = 0)
  63.         {                                            
  64.             int cnt = -1;
  65.             if (bs <= 0)
  66.             {
  67.                 byte[] inBytes = File.ReadAllBytes(inFile);
  68.                 File.WriteAllBytes(outFile, inBytes);
  69.                 return;
  70.             }
  71.  
  72.             long inFileLen = (new FileInfo(inFile)).Length;
  73.             byte[] byteBuf = new byte[bs];
  74.             using (FileStream inStr = new FileStream(inFile, FileMode.Open, FileAccess.Read))
  75.             {
  76.                 using (FileStream outStr = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write))
  77.                 {
  78.                     if (inStr != null && outStr != null)
  79.                     {
  80.                         while (bs * ++cnt < inFileLen)
  81.                         {
  82.                             int bRead = inStr.Read(byteBuf, 0, bs);
  83.                             if (bRead > 0)
  84.                                 outStr.Write(byteBuf, 0, bRead);
  85.                         }
  86.  
  87.                         inStr.Close();
  88.                         outStr.Flush();
  89.                         outStr.Close();
  90.                         Console.WriteLine($"{ProgName}\nread: \t{inFile}\nwrote: \t{outFile}\n" +
  91.                             $"bytes: \t{inFileLen},\trw-ops:\t{cnt} * {bs} blocksize.");
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement