Question: This below is a JAVA code i need help in making it a C# code. I I v import java.util.Scanner; import java.io.File; public class MyWindowsShell
This below is a JAVA code i need help in making it a C# code.
I
I
v
import java.util.Scanner; import java.io.File; public class MyWindowsShell { Scanner input = new Scanner(System.in); //to take command from user String command=""; String currentDirectory = System.getProperty("user.dir"); //finding current working directory void start() { System.out.println("Enter Your Command "); System.out.print(currentDirectory + "> "); while(true) //takes command untill user enters exit { command = input.nextLine(); if(command.equalsIgnoreCase("CLS")) CLS(); else if(command.indexOf("CD")==0 || command.indexOf("cd")==0) CD(); else if(command.indexOf("RENAME")==0 || command.indexOf("rename")==0) RENAME(); else if(command.equalsIgnoreCase("exit")) //write exit and press enter to exit from program break; } } void RENAME() { //find old file name from entered command. We assume ir is separated by single space. keep old filename without space int start = command.indexOf(" ", 0); int end = command.indexOf(" ", start+1); String oldName = command.substring(start+1, end);
//find new file name from entered command. We assume ir is separated by single space. keep new filename without space start = command.indexOf(" ", end); String newName = command.substring(start+1, command.length());
//make file object for OLD and NEW files File file = new File(currentDirectory + "\\" + oldName); File newFile = new File(currentDirectory + "\\" + newName);
//rename file if(file.renameTo(newFile)) //runs when rename successful { System.out.println("File rename success"); } else //when rename is not sucessful { System.out.println("File does not exists"); } System.out.print(currentDirectory + "> "); } void CD() { command = command.replace("CD",""); command = command.substring(1,command.length());
//set currect directory as per CD command File dir = new File(currentDirectory + "//" + command); //updated current working directory boolean exists = dir.exists(); ////checks if new directory exists if(exists) currentDirectory = currentDirectory + "\\" + command; else System.out.println("Directory does not exists"); System.out.print(currentDirectory + "> "); } void CLS() { //clear screen try { if (System.getProperty("os.name").contains("Windows")) //rum window command to clear screen { new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); } else { Runtime.getRuntime().exec("clear"); //for some OS this line of code clears screen Runtime.getRuntime().exec("cls"); //for some OS this line of code clears screen } } catch (Exception ex) {} System.out.print(currentDirectory + "> "); } public static void main(String args[]) { MyWindowsShell shell = new MyWindowsShell(); shell.start(); //starting application } }
THANKS IN ADVANCE
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
