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
投稿

猜你喜欢

  • java 算法之希尔排序详解及实现代码

    2022-07-12 23:09:45
  • java中Unsafe的使用讲解

    2022-08-02 08:08:18
  • Java8时间接口LocalDateTime详细用法

    2023-11-25 09:56:49
  • C#关闭指定名字进程的方法

    2021-11-23 23:59:42
  • JavaWeb中使用JavaMail实现发送邮件功能实例详解

    2023-01-07 13:54:37
  • Java泛型通配符的使用详解

    2022-07-08 14:08:43
  • JAVA序列化Serializable及Externalizable区别详解

    2022-04-04 10:49:09
  • Mybatis 入门之MyBatis环境搭建(第一篇)

    2023-03-15 16:09:32
  • Springboot实现Shiro整合JWT的示例代码

    2022-01-30 02:58:32
  • mybatis 对于生成的sql语句 自动加上单引号的情况详解

    2023-09-21 15:25:00
  • springboot maven 项目打包jar 最后名称自定义的教程

    2021-08-12 00:01:11
  • java读取resources文件详解及实现代码

    2022-07-15 15:11:30
  • java easyUI实现自定义网格视图实例代码

    2022-05-16 23:52:54
  • 使用String类型小数值转换为Long类型

    2023-04-14 10:34:56
  • 关于MyBatis模糊查询的几种实现方式

    2023-05-09 04:23:12
  • 一口气说出Java 6种延时队列的实现方法(面试官也得服)

    2022-12-15 16:40:12
  • java中单例模式讲解

    2022-05-22 14:24:07
  • 了解Java线程池创建过程

    2022-09-29 20:45:49
  • mybatis防止SQL注入的方法实例详解

    2022-08-14 03:06:57
  • 关于mybatis使用${}时sql注入的问题

    2023-04-18 03:29:40
  • asp之家 软件编程 m.aspxhome.com