注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

OC/C/C++ 水墨上仙 1810次浏览 已收录 手机上查看

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

using System;
using System.IO;
using System.Linq;
namespace RobvanderWoude
{
	class CloneDate
	{
		static int Main( string[] args )
		{
			bool debug = false;
			switch ( args.Length )
			{
				case 0:
					return WriteError( string.Empty );
				case 2:
					break;
				case 3:
					if ( args[2].Substring( 0, 2 ).ToUpper( ) == "/D" )
					{
						debug = true;
					}
					else
					{
						return WriteError( "Invalid command line argument(s)" );
					}
					break;
				default:
					return WriteError( "Invalid number of command line arguments" );
			}
			try
			{
				string sourcefile = args[0];
				// Check if a source file was specified
				if ( string.IsNullOrWhiteSpace( sourcefile ) )
				{
					return WriteError( "Invalid source file specification" );
				}
				// Check if the source file name is valid, and make sure to use its full path
				try
				{
					sourcefile = Path.GetFullPath( sourcefile ).Trim( '"' );
				}
				catch ( ArgumentException )
				{
					return WriteError( "No wildcards allowed in source file" );
				}
				// Check if the source file exists
				if ( !File.Exists( sourcefile ) )
				{
					return WriteError( "File not found: \"" + sourcefile + "\"" );
				}
				string targetspec = args[1];
				if ( string.IsNullOrWhiteSpace( targetspec ) )
				{
					return WriteError( "Invalid target file specification" );
				}
				// Check if the target directory exists
				string targetdir = string.Empty;
				try
				{
					targetdir = Path.GetDirectoryName( targetspec );
					if ( string.IsNullOrWhiteSpace( targetdir ) )
					{
						targetdir = Path.GetFullPath( "." );
					}
				}
				catch ( ArgumentException )
				{
					return WriteError( "Target folder not found: \"" + targetspec + "\"" );
				}
				// Extract the FILE specification (removing the path)
				string targetfilespec = string.Empty;
				if ( targetspec.IndexOf( "\\" ) == -1 )
				{
					targetfilespec = targetspec;
				}
				else
				{
					targetfilespec = targetspec.Substring( targetspec.LastIndexOf( "\\" ) + 1 );
				}
				string[] targetfiles = Directory.EnumerateFiles( targetdir, targetfilespec ).ToArray<string>( );
				DateTime timestamp = File.GetLastWriteTime( sourcefile );
				int count = 0;
				foreach ( string targetfile in targetfiles )
				{
					if ( targetfile.ToUpper( ) != sourcefile.ToUpper( ) )
					{
						count++;
						if ( debug )
						{
							Console.WriteLine( "File   : {0}", targetfile );
							Console.WriteLine( "Before : {0}", File.GetLastWriteTime( targetfile ) );
						}
						File.SetLastWriteTime( targetfile, timestamp );
						if ( debug )
						{
							Console.WriteLine( "After  : {0}", File.GetLastWriteTime( targetfile ) );
							Console.WriteLine( );
						}
					}
				}
				
				if ( debug )
				{
					Console.WriteLine( "{0} matching file{1}", count, ( count == 1 ? "" : "s" ) );
				}
				if ( count == 0 )
				{
					return WriteError( "No matching target files: \"" + targetspec + "\"" );
				}
				return 0;
			}
			catch ( Exception e )
			{
				return WriteError( e.Message );
			}
		}
		public static int WriteError( Exception e )
		{
			return WriteError( e == null ? null : e.Message );
		}
		public static int WriteError( string errorMessage )
		{
			/*
			CloneDate.exe,  Version 0.50 BETA
			Modify the LastModified date (timestamp) of the target file(s) to
			match the specified source file's timestamp
			Usage:    CloneDate.exe  sourcefile  targetfiles  [ /Debug ]
			Where:    sourcefile     is the file whose timestamp is to be cloned
			          targetfiles    are the files whose timestamp are to be modified
			                         (single filespec, wildcards * and ? are allowed)
			          /Debug         displays file name and timestamps before and
			                         after modification for each matching file
			Example:  CloneDate.exe C:\boot.ini C:\test.log
			          will change C:\test.log's timestamp to match C:\boot.ini's
			Notes:    Target filespec may include sourcefile (sourcefile will be skipped).
			          Always be careful when using wildcards; they may also return matching
			          "short" (8.3) file names (for backwards compatibility with FAT16).
			Written by Rob van der Woude
			http://www.robvanderwoude.com
			*/
			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
			{
				Console.Error.WriteLine( );
				Console.ForegroundColor = ConsoleColor.Red;
				Console.Error.Write( "ERROR: " );
				Console.ForegroundColor = ConsoleColor.White;
				Console.Error.WriteLine( errorMessage );
				Console.ResetColor( );
			}
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "CloneDate.exe,  Version 0.50 BETA" );
			Console.Error.WriteLine( "Modify the LastModified date (timestamp) of the target\nfile(s) to match the specified source file's timestamp" );
			Console.Error.WriteLine( );
			Console.Error.Write( "Usage:    " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "CloneDate.exe  sourcefile  targetfiles" );
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.Write( "Where:    " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "sourcefile" );
			Console.ResetColor( );
			Console.Error.WriteLine( "     is the file whose timestamp is to be cloned" );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "          targetfiles" );
			Console.ResetColor( );
			Console.Error.WriteLine( "    are the files whose timestamp are to be modified\n                         (single filespec, wildcards * and ? are allowed)" );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "          /D" );
			Console.ResetColor( );
			Console.Error.WriteLine( "ebug         displays file name and timestamps before and\n                         after modification for each matching file" );
			Console.Error.WriteLine( );
			Console.Error.Write( "Example:  " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "CloneDate.exe C:\\boot.ini C:\\test.log" );
			Console.ResetColor( );
			Console.Error.WriteLine( "          will change C:\\test.log's timestamp to match C:\\boot.ini's" );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Notes:    Target filespec may include sourcefile (sourcefile will be skipped)." );
			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)." );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Written by Rob van der Woude" );
			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
			return 1;
		}
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C# 修改指定文件的最后修改时间的时间戳
喜欢 (0)
[开心洋葱]
分享 (0)
关于作者:
水墨上仙
……
加载中……