国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#將指定目錄所有文件名轉換成小寫的方法

2020-01-24 01:58:41
字體:
來源:轉載
供稿:網友

本文實例講述了C#將指定目錄所有文件名轉換成小寫的方法。分享給大家供大家參考。具體如下:

using System;using System.IO;using System.Linq;namespace RobvanderWoude{ class LoCase {  static int Main( string[] args )  {   string dir = string.Empty;   string filespec = string.Empty;   char[] trailingbackslash = "http://".ToCharArray( 0, 1 );   char[] upcaseletters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray( 0, 26 );   bool verbose = false;   #region Command Line Parsing   switch ( args.Length )   {    case 0:     return WriteError( string.Empty );    case 1:     filespec = args[0].Trim( '"' );     break;    case 2:     filespec = args[0].Trim( '"' );     if ( args[1].Substring( 0, 2 ).ToUpper( ) == "/V" )     {      verbose = true;     }     else     {      return WriteError( "Invalid command line switch: " + args[1] );     }     break;    default:     return WriteError( string.Empty );   }   if ( string.IsNullOrWhiteSpace( filespec ) || filespec == "/?" )   {    return WriteError( string.Empty );   }   if ( filespec.IndexOfAny( "/?".ToCharArray( 0, 2 ) ) != -1 )   {    return WriteError( "Invalid file specification: /"" + filespec + "/"" );   }   #endregion Command Line Parsing   try   {    // Check if the directory exists    try    {     dir = Path.GetDirectoryName( filespec );     if ( string.IsNullOrWhiteSpace( dir ) )     {      dir = Path.GetFullPath( "." );     }     if ( !Directory.Exists( dir ) )     {      return WriteError( "Directory not found: /"" + dir + "/"" );     }     dir = dir.TrimEnd( trailingbackslash ) + "http://";    }    catch ( ArgumentException )    {     return WriteError( "Parent directory not found" );    }    // Extract the FILE specification (removing the path)    string filenames = filespec.Substring( filespec.LastIndexOf( "http://" ) + 1 );    // Enumerate the files    string[] files = Directory.EnumerateFiles( dir, filenames ).ToArray<string>( );    int count = 0;    foreach ( string file in files )    {     if ( File.Exists( file ) )     {      string filename = Path.GetFileName( file );      if ( filename.IndexOfAny( upcaseletters ) > -1 )      {       count++;       string newfilename = dir + filename.ToLowerInvariant( );       File.Move( file, newfilename );      }     }    }    if ( verbose )    {     Console.WriteLine( "{0} matching file{1} renamed", ( count == 0 ? "No" : count.ToString( ) ), ( count == 1 ? string.Empty : "s" ) );    }    return count;   }   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 )  {   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( "LoCase.exe, Version 1.02" );   Console.Error.WriteLine( "Rename specified files to all lower case" );   Console.Error.WriteLine( );   Console.Error.Write( "Usage: " );   Console.ForegroundColor = ConsoleColor.White;   Console.Error.WriteLine( "LoCase.exe filespec [ /Verbose ]" );   Console.ResetColor( );   Console.Error.WriteLine( );   Console.Error.Write( "Where: " );   Console.ForegroundColor = ConsoleColor.White;   Console.Error.Write( "filespec" );   Console.ResetColor( );   Console.Error.WriteLine( " is (are) the file(s) to be renamed (wildcards allowed)" );   Console.ForegroundColor = ConsoleColor.White;   Console.Error.Write( "   /V" );   Console.ResetColor( );   Console.Error.WriteLine( "erbose displays the number of files renamed" );   Console.Error.WriteLine( );   Console.Error.WriteLine( "Note:  Use doublequotes if filespec contains spaces." );   Console.Error.WriteLine( "   Return code (/"ErrorLevel/") equals the number of renamed files." );   Console.Error.Write( "   Switch may be abbreviated, e.g. " );   Console.ForegroundColor = ConsoleColor.White;   Console.Error.Write( "/V" );   Console.ResetColor( );   Console.Error.Write( " instead of " );   Console.ForegroundColor = ConsoleColor.White;   Console.Error.Write( "/V" );   Console.ResetColor( );   Console.Error.WriteLine( "erbose." );   Console.Error.WriteLine( );   Console.Error.WriteLine( "Written by Rob van der Woude" );   return 0;  } }}

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉木萨尔县| 临颍县| 洛宁县| 饶河县| 息烽县| 安乡县| 张家口市| 读书| 温州市| 新兴县| 松阳县| 磐石市| 土默特左旗| 安西县| 卫辉市| 泗水县| 定安县| 阿坝| 沙雅县| 新和县| 改则县| 株洲县| 泸西县| 治多县| 突泉县| 张家界市| 丰都县| 广元市| 高淳县| 金湖县| 庆安县| 沭阳县| 平度市| 白河县| 房产| 曲阜市| 东阳市| 奉贤区| 沂水县| 涞源县| 南投县|