C#将制定目录文件名转换成大写的方法

作者:work24 时间:2022-10-03 19:46:36 

本文实例讲述了C#将制定目录文件名转换成大写的方法。分享给大家供大家参考。具体如下:


using System;
using System.IO;
using System.Linq;
namespace RobvanderWoude
{
class UpCase
{
 static int Main( string[] args )
 {
  string dir = string.Empty;
  string filespec = string.Empty;
  char[] trailingbackslash = "\\".ToCharArray( 0, 1 );
  char[] locaseletters = "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( locaseletters ) > -1 )
     {
      count++;
      string newfilename = dir + filename.ToUpperInvariant( );
      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 )
 {
  /*
  UpCase.exe, Version 1.02
  Rename specified files to all upper case
  Usage:  UpCase.exe filespec [ /Verbose ]
  Where:  filespec  is (are) the file(s) to be renamed (wildcards allowed)
       /Verbose  displays the number of files renamed
  Notes:  Use doublequotes if filespec contains spaces.
       Return code (\"ErrorLevel\") equals the number of renamed files.
       Switch may be abbreviated, e.g. /V instead of /Verbose.
  Written by Rob van der Woude
  */
  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( "UpCase.exe, Version 1.02" );
  Console.Error.WriteLine( "Rename specified files to all upper case" );
  Console.Error.WriteLine( );
  Console.Error.Write( "Usage:  " );
  Console.ForegroundColor = ConsoleColor.White;
  Console.Error.WriteLine( "UpCase.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" );
  Console.Error.WriteLine( "http://www.baidu.com" );
  return 0;
 }
}
}

希望本文所述对大家的C#程序设计有所帮助。

标签:C#,转换,大写
0
投稿

猜你喜欢

  • SuperSocket入门--Telnet服务器和客户端请求处理

    2021-07-24 19:35:14
  • Java详细分析梳理垃圾回收机制

    2023-10-30 04:02:33
  • Java Benchmark 基准测试的实例详解

    2023-10-08 11:01:02
  • 深入理解Javascript中的this关键字

    2022-03-08 16:52:25
  • Java实现读取文章中重复出现的中文字符串

    2022-04-27 04:29:05
  • Java并发编程面试之线程池

    2023-11-11 10:58:33
  • 详解spring-boot集成elasticsearch及其简单应用

    2021-08-26 01:09:58
  • 安卓(Android)实现选择时间功能

    2023-11-05 08:20:58
  • java 中的instanceof用法详解及instanceof是什么意思(推荐)

    2023-06-07 13:52:27
  • 解决C#调用dll提示

    2022-11-30 21:46:39
  • 如何优雅的处理Spring Boot异常信息详解

    2023-11-29 09:50:02
  • 实例分析java开启线程的方法

    2022-06-24 19:54:00
  • SpringMVC实现注解式权限验证的实例

    2021-11-19 11:52:44
  • java打印正弦曲线示例

    2022-05-04 00:48:56
  • Android Studio 3.0的下载安装教程

    2021-08-10 04:00:45
  • 使用newInstance()来实例化fragment并传递数据操作

    2022-07-11 15:41:57
  • Java简单工厂模式详细解释

    2021-09-20 16:12:07
  • 解决Android Studio4.1没有Gsonfomat插件,Plugin “GsonFormat” is incompatible的问题

    2021-08-27 22:10:37
  • Java8处理集合的优雅姿势之Stream

    2023-03-14 09:31:48
  • C# List介绍及具体用法

    2021-09-15 07:59:14
  • asp之家 软件编程 m.aspxhome.com