时间:2021-05-20
本文实例讲述了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 = "\\".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#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#将制定目录文件名转换成大写的方法。分享给大家供大家参考。具体如下:usingSystem;usingSystem.IO;usingSystem
在c#中可以遍历指定驱动器或指定目录下嵌套目录中的所有文件或者任意深度的文件。通过遍历可以检索string形式的目录名和文件名,也可以检索System.IO.F
使用C#在不借助第三方插件的情况下将Excel中的数据转换成DataSet//////EXCEL数据转换DataSet//////文件全路径///表名///pr
一、用C#将Image转换成byte[]并插入数据库:1.1 将图片控件的Image转换成流:复制代码代码如下:privatebyte[]PicToArray(
本文实例讲述了C#编程实现获取文件夹中所有文件的文件名。分享给大家供大家参考,具体如下:想实现这样一个功能:批量修改一个目录所有jpg文件的文件名,在其原始基础