[C#] C# 修改指定文件的最后修改时间的时间戳 →→→→→进入此内容的聊天室

来自 , 2019-05-19, 写在 C#, 查看 121 次.
URL http://www.code666.cn/view/0e189c35
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. namespace RobvanderWoude
  6. {
  7.         class CloneDate
  8.         {
  9.                 static int Main( string[] args )
  10.                 {
  11.                         bool debug = false;
  12.                         switch ( args.Length )
  13.                         {
  14.                                 case 0:
  15.                                         return WriteError( string.Empty );
  16.                                 case 2:
  17.                                         break;
  18.                                 case 3:
  19.                                         if ( args[2].Substring( 0, 2 ).ToUpper( ) == "/D" )
  20.                                         {
  21.                                                 debug = true;
  22.                                         }
  23.                                         else
  24.                                         {
  25.                                                 return WriteError( "Invalid command line argument(s)" );
  26.                                         }
  27.                                         break;
  28.                                 default:
  29.                                         return WriteError( "Invalid number of command line arguments" );
  30.                         }
  31.  
  32.                         try
  33.                         {
  34.                                 string sourcefile = args[0];
  35.                                 // Check if a source file was specified
  36.                                 if ( string.IsNullOrWhiteSpace( sourcefile ) )
  37.                                 {
  38.                                         return WriteError( "Invalid source file specification" );
  39.                                 }
  40.                                 // Check if the source file name is valid, and make sure to use its full path
  41.                                 try
  42.                                 {
  43.                                         sourcefile = Path.GetFullPath( sourcefile ).Trim( '"' );
  44.                                 }
  45.                                 catch ( ArgumentException )
  46.                                 {
  47.                                         return WriteError( "No wildcards allowed in source file" );
  48.                                 }
  49.                                 // Check if the source file exists
  50.                                 if ( !File.Exists( sourcefile ) )
  51.                                 {
  52.                                         return WriteError( "File not found: \"" + sourcefile + "\"" );
  53.                                 }
  54.  
  55.                                 string targetspec = args[1];
  56.                                 if ( string.IsNullOrWhiteSpace( targetspec ) )
  57.                                 {
  58.                                         return WriteError( "Invalid target file specification" );
  59.                                 }
  60.                                 // Check if the target directory exists
  61.                                 string targetdir = string.Empty;
  62.                                 try
  63.                                 {
  64.                                         targetdir = Path.GetDirectoryName( targetspec );
  65.                                         if ( string.IsNullOrWhiteSpace( targetdir ) )
  66.                                         {
  67.                                                 targetdir = Path.GetFullPath( "." );
  68.                                         }
  69.                                 }
  70.                                 catch ( ArgumentException )
  71.                                 {
  72.                                         return WriteError( "Target folder not found: \"" + targetspec + "\"" );
  73.                                 }
  74.                                 // Extract the FILE specification (removing the path)
  75.                                 string targetfilespec = string.Empty;
  76.                                 if ( targetspec.IndexOf( "\\" ) == -1 )
  77.                                 {
  78.                                         targetfilespec = targetspec;
  79.                                 }
  80.                                 else
  81.                                 {
  82.                                         targetfilespec = targetspec.Substring( targetspec.LastIndexOf( "\\" ) + 1 );
  83.                                 }
  84.                                 string[] targetfiles = Directory.EnumerateFiles( targetdir, targetfilespec ).ToArray<string>( );
  85.                                 DateTime timestamp = File.GetLastWriteTime( sourcefile );
  86.                                 int count = 0;
  87.                                 foreach ( string targetfile in targetfiles )
  88.                                 {
  89.                                         if ( targetfile.ToUpper( ) != sourcefile.ToUpper( ) )
  90.                                         {
  91.                                                 count++;
  92.                                                 if ( debug )
  93.                                                 {
  94.                                                         Console.WriteLine( "File   : {0}", targetfile );
  95.                                                         Console.WriteLine( "Before : {0}", File.GetLastWriteTime( targetfile ) );
  96.                                                 }
  97.                                                 File.SetLastWriteTime( targetfile, timestamp );
  98.                                                 if ( debug )
  99.                                                 {
  100.                                                         Console.WriteLine( "After  : {0}", File.GetLastWriteTime( targetfile ) );
  101.                                                         Console.WriteLine( );
  102.                                                 }
  103.                                         }
  104.                                 }
  105.                                
  106.                                 if ( debug )
  107.                                 {
  108.                                         Console.WriteLine( "{0} matching file{1}", count, ( count == 1 ? "" : "s" ) );
  109.                                 }
  110.  
  111.                                 if ( count == 0 )
  112.                                 {
  113.                                         return WriteError( "No matching target files: \"" + targetspec + "\"" );
  114.                                 }
  115.  
  116.                                 return 0;
  117.                         }
  118.                         catch ( Exception e )
  119.                         {
  120.                                 return WriteError( e.Message );
  121.                         }
  122.                 }
  123.  
  124.                 public static int WriteError( Exception e )
  125.                 {
  126.                         return WriteError( e == null ? null : e.Message );
  127.                 }
  128.  
  129.                 public static int WriteError( string errorMessage )
  130.                 {
  131.                         /*
  132.                         CloneDate.exe,  Version 0.50 BETA
  133.                         Modify the LastModified date (timestamp) of the target file(s) to
  134.                         match the specified source file's timestamp
  135.  
  136.                         Usage:    CloneDate.exe  sourcefile  targetfiles  [ /Debug ]
  137.  
  138.                         Where:    sourcefile     is the file whose timestamp is to be cloned
  139.                                   targetfiles    are the files whose timestamp are to be modified
  140.                                                  (single filespec, wildcards * and ? are allowed)
  141.                                   /Debug         displays file name and timestamps before and
  142.                                                  after modification for each matching file
  143.  
  144.                         Example:  CloneDate.exe C:\boot.ini C:\test.log
  145.                                   will change C:\test.log's timestamp to match C:\boot.ini's
  146.  
  147.                         Notes:    Target filespec may include sourcefile (sourcefile will be skipped).
  148.                                   Always be careful when using wildcards; they may also return matching
  149.                                   "short" (8.3) file names (for backwards compatibility with FAT16).
  150.  
  151.                         Written by Rob van der Woude
  152.                         http://www.robvanderwoude.com
  153.                         */
  154.  
  155.                         if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  156.                         {
  157.                                 Console.Error.WriteLine( );
  158.                                 Console.ForegroundColor = ConsoleColor.Red;
  159.                                 Console.Error.Write( "ERROR: " );
  160.                                 Console.ForegroundColor = ConsoleColor.White;
  161.                                 Console.Error.WriteLine( errorMessage );
  162.                                 Console.ResetColor( );
  163.                         }
  164.                         Console.Error.WriteLine( );
  165.                         Console.Error.WriteLine( "CloneDate.exe,  Version 0.50 BETA" );
  166.                         Console.Error.WriteLine( "Modify the LastModified date (timestamp) of the target\nfile(s) to match the specified source file's timestamp" );
  167.                         Console.Error.WriteLine( );
  168.                         Console.Error.Write( "Usage:    " );
  169.                         Console.ForegroundColor = ConsoleColor.White;
  170.                         Console.Error.WriteLine( "CloneDate.exe  sourcefile  targetfiles" );
  171.                         Console.ResetColor( );
  172.                         Console.Error.WriteLine( );
  173.                         Console.Error.Write( "Where:    " );
  174.                         Console.ForegroundColor = ConsoleColor.White;
  175.                         Console.Error.Write( "sourcefile" );
  176.                         Console.ResetColor( );
  177.                         Console.Error.WriteLine( "     is the file whose timestamp is to be cloned" );
  178.                         Console.ForegroundColor = ConsoleColor.White;
  179.                         Console.Error.Write( "          targetfiles" );
  180.                         Console.ResetColor( );
  181.                         Console.Error.WriteLine( "    are the files whose timestamp are to be modified\n                         (single filespec, wildcards * and ? are allowed)" );
  182.                         Console.ForegroundColor = ConsoleColor.White;
  183.                         Console.Error.Write( "          /D" );
  184.                         Console.ResetColor( );
  185.                         Console.Error.WriteLine( "ebug         displays file name and timestamps before and\n                         after modification for each matching file" );
  186.                         Console.Error.WriteLine( );
  187.                         Console.Error.Write( "Example:  " );
  188.                         Console.ForegroundColor = ConsoleColor.White;
  189.                         Console.Error.WriteLine( "CloneDate.exe C:\\boot.ini C:\\test.log" );
  190.                         Console.ResetColor( );
  191.                         Console.Error.WriteLine( "          will change C:\\test.log's timestamp to match C:\\boot.ini's" );
  192.                         Console.Error.WriteLine( );
  193.                         Console.Error.WriteLine( "Notes:    Target filespec may include sourcefile (sourcefile will be skipped)." );
  194.                         Console.Error.WriteLine( "          Always be careful when using wildcards; they may also return matching\n          \"short\" (8.3) file names (for backwards compatibility with FAT16)." );
  195.                         Console.Error.WriteLine( );
  196.                         Console.Error.WriteLine( "Written by Rob van der Woude" );
  197.                         Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  198.                         return 1;
  199.                 }
  200.  
  201.         }
  202. }
  203. //csharp/7314

回复 "C# 修改指定文件的最后修改时间的时间戳"

这儿你可以回复上面这条便签

captcha