• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#将制定目录文件名转换成大写的方法

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

作者:work24 字体:[增加 减小] 来源:互联网 时间:2017-05-28

work24 通过本文主要向大家介绍了c#当前目录,c#取运行目录,c#复制文件到指定目录,c#根目录,c#删除目录下所有文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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;
  }
 }
}
</div>

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#遍历子目录的方法
  • C#将制定目录文件名转换成大写的方法
  • C# 递归查找树状目录实现方法

相关文章

  • 2017-05-28C#中IEnumerable、ICollection、IList、List之间的区别
  • 2017-05-28c# 接口interface基础入门小例子
  • 2017-05-28C#实现图形位置组合转换的方法
  • 2017-05-28C#中csv文件与DataTable互相导入处理实例解析
  • 2017-05-28C# 站点IP访问频率限制 针对单个站点的实现方法
  • 2017-05-28C#调用SQL语句时乘号的用法
  • 2017-05-28C#常用正则验证函数示例
  • 2017-05-28js substr,substring与java substring和C# substring的区别解析
  • 2017-05-28重温C# clr 笔记总结
  • 2017-05-28将ocx文件转换成C#程序引用的DLL文件的办法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 解析abstract与override究竟可不可以同时使用
    • C#验证码问题的快速解决方法
    • 浅析C#的复制和克隆
    • C#中的问号(?号)用法小结
    • C#写日志类实例
    • C#获取指定PDF文件页数的方法
    • C# TreeView读取数据库简单实例
    • 共享锁using范围的实现方法
    • 基于XSLT调试的相关问题
    • c#对list排序示例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有