C#将制定目录文件名转换成大写的方法
本文实例讲述了C#将制定目录文件名转换成大写的方法。分享给大家供大家参考。具体如下:
usingSystem; usingSystem.IO; usingSystem.Linq; namespaceRobvanderWoude { classUpCase { staticintMain(string[]args) { stringdir=string.Empty; stringfilespec=string.Empty; char[]trailingbackslash="\\".ToCharArray(0,1); char[]locaseletters="abcdefghijklmnopqrstuvwxyz".ToCharArray(0,26); boolverbose=false; #regionCommandLineParsing switch(args.Length) { case0: returnWriteError(string.Empty); case1: filespec=args[0].Trim('"'); break; case2: filespec=args[0].Trim('"'); if(args[1].Substring(0,2).ToUpper()=="/V") { verbose=true; } else { returnWriteError("Invalidcommandlineswitch:"+args[1]); } break; default: returnWriteError(string.Empty); } if(string.IsNullOrWhiteSpace(filespec)||filespec=="/?") { returnWriteError(string.Empty); } if(filespec.IndexOfAny("/?".ToCharArray(0,2))!=-1) { returnWriteError("Invalidfilespecification:\""+filespec+"\""); } #endregionCommandLineParsing try { //Checkifthedirectoryexists try { dir=Path.GetDirectoryName(filespec); if(string.IsNullOrWhiteSpace(dir)) { dir=Path.GetFullPath("."); } if(!Directory.Exists(dir)) { returnWriteError("Directorynotfound:\""+dir+"\""); } dir=dir.TrimEnd(trailingbackslash)+"\\"; } catch(ArgumentException) { returnWriteError("Parentdirectorynotfound"); } //ExtracttheFILEspecification(removingthepath) stringfilenames=filespec.Substring(filespec.LastIndexOf("\\")+1); //Enumeratethefiles string[]files=Directory.EnumerateFiles(dir,filenames).ToArray<string>(); intcount=0; foreach(stringfileinfiles) { if(File.Exists(file)) { stringfilename=Path.GetFileName(file); if(filename.IndexOfAny(locaseletters)>-1) { count++; stringnewfilename=dir+filename.ToUpperInvariant(); File.Move(file,newfilename); } } } if(verbose) { Console.WriteLine("{0}matchingfile{1}renamed",(count==0?"No":count.ToString()),(count==1?string.Empty:"s")); } returncount; } catch(Exceptione) { returnWriteError(e.Message); } } publicstaticintWriteError(Exceptione) { returnWriteError(e==null?null:e.Message); } publicstaticintWriteError(stringerrorMessage) { /* UpCase.exe,Version1.02 Renamespecifiedfilestoalluppercase Usage:UpCase.exefilespec[/Verbose] Where:filespecis(are)thefile(s)toberenamed(wildcardsallowed) /Verbosedisplaysthenumberoffilesrenamed Notes:Usedoublequotesiffilespeccontainsspaces. Returncode(\"ErrorLevel\")equalsthenumberofrenamedfiles. Switchmaybeabbreviated,e.g./Vinsteadof/Verbose. WrittenbyRobvanderWoude */ 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,Version1.02"); Console.Error.WriteLine("Renamespecifiedfilestoalluppercase"); Console.Error.WriteLine(); Console.Error.Write("Usage:"); Console.ForegroundColor=ConsoleColor.White; Console.Error.WriteLine("UpCase.exefilespec[/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)thefile(s)toberenamed(wildcardsallowed)"); Console.ForegroundColor=ConsoleColor.White; Console.Error.Write("/V"); Console.ResetColor(); Console.Error.WriteLine("erbosedisplaysthenumberoffilesrenamed"); Console.Error.WriteLine(); Console.Error.WriteLine("Note:Usedoublequotesiffilespeccontainsspaces."); Console.Error.WriteLine("Returncode(\"ErrorLevel\")equalsthenumberofrenamedfiles."); Console.Error.Write("Switchmaybeabbreviated,e.g."); Console.ForegroundColor=ConsoleColor.White; Console.Error.Write("/V"); Console.ResetColor(); Console.Error.Write("insteadof"); Console.ForegroundColor=ConsoleColor.White; Console.Error.Write("/V"); Console.ResetColor(); Console.Error.WriteLine("erbose."); Console.Error.WriteLine(); Console.Error.WriteLine("WrittenbyRobvanderWoude"); Console.Error.WriteLine("http://www.baidu.com"); return0; } } }
希望本文所述对大家的C#程序设计有所帮助。