欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年04月17日 15:38:17   作者:work24  
這篇文章主要介紹了C#將指定目錄所有文件名轉(zhuǎn)換成小寫的方法,涉及C#中目錄的遍歷及filename.ToLowerInvariant方法的使用技巧,非常具有實用價值,需要的朋友可以參考下

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

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 = "\\".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 ) + "\\";
    }
    catch ( ArgumentException )
    {
     return WriteError( "Parent directory not found" );
    }
    // Extract the FILE specification (removing the path)
    string filenames = filespec.Substring( filespec.LastIndexOf( "\\" ) + 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#程序設(shè)計有所幫助。

相關(guān)文章

  • C#的回調(diào)機制淺析

    C#的回調(diào)機制淺析

    這篇文章主要介紹了C#的回調(diào)機制,較為通俗的分析了回調(diào)機制的本質(zhì)與使用時的注意事項,對于C#的學(xué)習(xí)有一定的參考借鑒價值,需要的朋友可以參考下
    2014-11-11
  • C#實現(xiàn)飛行棋源碼

    C#實現(xiàn)飛行棋源碼

    這篇文章主要為大家詳細介紹了C#實現(xiàn)飛行棋源碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 一文帶你了解 C# DLR 的世界(DLR 探秘)

    一文帶你了解 C# DLR 的世界(DLR 探秘)

    DLR 是 C#4.0 新引進來的概念,其主要目的就是為了動態(tài)綁定與交互。下面小編給大家?guī)硪黄诲e教程,一文帶你了解 C# DLR 的世界,需要的朋友可以參考下
    2020-02-02
  • WinForm中DefWndProc、WndProc與IMessageFilter的區(qū)別

    WinForm中DefWndProc、WndProc與IMessageFilter的區(qū)別

    這篇文章主要介紹了WinForm中DefWndProc、WndProc與IMessageFilter的區(qū)別,較為詳細的分析了WinForm的消息處理機制,需要的朋友可以參考下
    2014-08-08
  • C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則的示例詳解

    C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則的示例詳解

    在面向?qū)ο缶幊讨?,SOLID?是五個設(shè)計原則的首字母縮寫,旨在使軟件設(shè)計更易于理解、靈活和可維護。本文將通過實例詳細講講C#面向?qū)ο缶幊讨幸蕾嚪崔D(zhuǎn)原則,需要的可以參考一下
    2022-07-07
  • C#通過委托調(diào)用Button單擊事件的方法

    C#通過委托調(diào)用Button單擊事件的方法

    本文給大家分享的是通過委托取消Button事件switch-case的方法,十分的簡單實用,有需要的小伙伴可以參考下。
    2015-05-05
  • 詳解WPF如何動態(tài)生成DataGrid的行和列

    詳解WPF如何動態(tài)生成DataGrid的行和列

    在日常開發(fā)中,DataGrid作為二維表格,非常適合數(shù)據(jù)的展示和統(tǒng)計,本文以一些簡單的小例子,簡述在WPF開發(fā)中,如何動態(tài)生成DataGrid的行和列,需要的可以了解下
    2024-02-02
  • Unity 如何批量修改FBX模型

    Unity 如何批量修改FBX模型

    這篇文章主要介紹了Unity 批量修改FBX模型的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • c# 實現(xiàn)模糊PID控制算法

    c# 實現(xiàn)模糊PID控制算法

    這篇文章主要介紹了c# 實現(xiàn)模糊PID控制算法的示例代碼,幫助大家更好的理解和使用c#編程語言,感興趣的朋友可以了解下
    2020-12-12
  • C#實現(xiàn)拼手氣紅包算法

    C#實現(xiàn)拼手氣紅包算法

    這篇文章主要為大家詳細介紹了C#實現(xiàn)拼手氣紅包算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09

最新評論