Csharp/C Sharp/File Stream/File Command

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

C# Implementation of the File Copy Command

using System;
using System.IO;
  class Class1 {
    static void Main(string[] args) {
      string [] cla  = Environment.GetCommandLineArgs();
      if (cla.GetUpperBound(0) == 2) {
        FileInfo fi = new FileInfo(cla[1]);
        fi.CopyTo(cla[2],true);
        Console.WriteLine("Copied " + fi.Length + " bytes.");
      }
      else{
        Console.WriteLine ("Usage: cp <input file> <output file>");
      }
    }
  }


File Move Implementation

using System;
using System.IO;
  class Class1 {
    static void Main(string[] args) {
      string [] cla  = Environment.GetCommandLineArgs();
      if (cla.GetUpperBound(0) == 2)
      {
        FileInfo fi = new FileInfo(cla[1]);
        fi.MoveTo(cla[2]);
        Console.WriteLine("File Created : " + fi.CreationTime.ToString());
        Console.WriteLine("Moved to     : " + cla[2]);
      }
      else
        Console.WriteLine ("Usage: mv <source file> <destination file>");
    }
  }


Use the FileInfo Class to Delete Files with Ease

using System;
using System.IO;

  class Class1 {
    static void Main(string[] args) {
      string [] cla  = Environment.GetCommandLineArgs();
      if (cla.GetUpperBound(0) == 1)
      {
        FileInfo fi = new FileInfo(cla[1]);
        fi.Delete();
        Console.WriteLine("File      : " + cla[1]);
        Console.WriteLine("Attributes: " + fi.Attributes.ToString());
        Console.WriteLine("File Deleted...");
      }
      else
        Console.WriteLine ("Usage: rm <filename>");
    }
  }