using System; using System.IO; using System.Security; class MyExplorer { public static void Main(string[] args) { MyExplorer oExplorer=new MyExplorer(); String Command; String[] CommandLineArgs; int ArgumentLength; bool CanExit=false; Console.WriteLine("Welcome to the MyExplorer Dos Utility"); do { Console.Write("MyExplorer>"); Command=Console.ReadLine(); Command=ToSingleSpace(Command.Trim().ToLower()); CommandLineArgs=oExplorer.ParseCommand(Command.ToLower(),out ArgumentLength); switch(CommandLineArgs[0]) { case "cpy": if(ArgumentLength!=2) { ShowError("Usage cpy "); } else { oExplorer.Copy(CommandLineArgs[1],CommandLineArgs[2]); } break; case "rem": if(ArgumentLength!=1) { ShowError("Usage rem "); } else { oExplorer.Remove(CommandLineArgs[1]); } break; case "dir": case "ls": switch(ArgumentLength) { case 0: oExplorer.ListFiles("."); break; case 1: oExplorer.ListFiles(CommandLineArgs[1]); break; default: ShowError("Usage dir []"); } break; case "help": case "h": oExplorer.ShowHelp(); break; case "quit": case "exit": case "bye": CanExit=true; break; default: ShowError("Invalid Command"); } }while(!CanExit); } public static void ShowError(string StrError) { Console.WriteLine(StrError); } public static String ToSingleSpace(string StrToReplace) { int Position; Position=StrToReplace.IndexOf(" "); if(Position==-1) return StrToReplace; else return ToSingleSpace(StrToReplace.Substring(0,Position) + StrToReplace.Substring(Position+1)); } public string[] ParseCommand(String Command,out int ArgumentLength) { string[] CommandLineArgs=Command.Split(new char[]{' '}); ArgumentLength=CommandLineArgs.Length-1; return CommandLineArgs; } /* Function to the Usage of MyExplorer */ public void ShowHelp() { Console.WriteLine("MyExplorer Help Contents"); Console.WriteLine("========================="); Console.WriteLine("cpy - Copies the content of source to dest"); Console.WriteLine("rem - Filename to be deleted"); Console.WriteLine("dir/ls [] - Directory Listing"); Console.WriteLine("h/help - Help for using MyExplorer"); Console.WriteLine("quit/exit/bye - Exit MyExplorer"); } /* Method to perform File Copy operation */ public void Copy(string SourceFile,string DestinationFile) { try { File.Copy(SourceFile,DestinationFile,true); Console.WriteLine("File Copied Successfully"); } catch(FileNotFoundException) { Console.WriteLine("Source File does not Exists"); } catch(ArgumentException) { Console.WriteLine("Invalid Argument for cpy"); } catch(SecurityException) { Console.WriteLine("Write Access is Denied"); } catch(Exception) { Console.WriteLine("UnKnown Exception"); } } /* Function to Delete the Specified File */ public void Remove(string FileToRemove) { try { File.Delete(FileToRemove); Console.WriteLine("File Removed Successfully"); } catch(DirectoryNotFoundException) { Console.WriteLine("File does not exists"); } catch(SecurityException) { Console.WriteLine("Delete Permission Denied"); } catch(AccessException) { Console.WriteLine("Directory name is specified where file is required"); } catch(Exception) { Console.WriteLine("Unknown Exception"); } } /* Directory Listing */ public void ListFiles(string StrDirectoryToList) { Directory oDir; FileSystemEntry[] oFiles; try { oDir=new Directory(StrDirectoryToList); oFiles=oDir.GetFileSystemEntries(); bool IsFileFound=true; foreach(FileSystemEntry f in oFiles) { if(IsFileFound) { Console.WriteLine("{0,-32}{1,-8}{2}","Name","Size","Diretory/File"); Console.WriteLine(new string('=',80)); IsFileFound=false; } if(f.IsFile) { Console.WriteLine("{0,-32}{1,-8}{2}",f.Name,((File)f).Length,"F"); } else { Console.WriteLine("{0,-32}{1,-8}{2}",f.Name,"0","D"); } } } catch(DirectoryNotFoundException) { Console.WriteLine("Directory Not Found"); } catch(Exception) { } } }