Create a Console application that behaves like the dos shell. And should work in the same way as the command prompt of windows. Your shell should support the following commands:
cd
dir
del
xcopy
rd
ren
md
exit
copy
Create a Console application that behaves like the dos shell. And should work in the same way as the command prompt of windows. Your shell should support the following commands:
cd
dir
del
xcopy
rd
ren
md
exit
copy
>how Console app acts as a DOS shell.
Show us your code work please.
Please read before posting - http://www.daniweb.com/forums/thread78223.html and http://www.daniweb.com/forums/faq.php?faq=daniweb_policies
here is the code ,with using SWITCH but my sir is demanding to do this using IF-ELSE statments ,im a beginer ,so ihv got no idea how to do it .........ihv got a deadline till sunday,,,plz plz help me with it ,,,thnx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace dos_commands
{
class DosApp
{
string curPath;
bool isExitEntered;
string input;
public DosApp()
{
this.curPath = "D:\\";
this.isExitEntered = false;
}
public void Start()
{
Console.WriteLine("Welcome to Dos ");
while (!this.isExitEntered)
{
Console.Write(this.curPath + ">");
this.input = Console.ReadLine();
string[] tokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
ExecuteCommand(tokens);
}
}
void ExecuteCommand(string[] tokens)
{
string p;
if (tokens.Length > 0)
{
string code = tokens[0].ToLower();
switch (code)
{
case "exit":
this.isExitEntered = true;
break;
case "dir":
Console.WriteLine("enter path");
p = Console.ReadLine();
DirectoryInfo di = new DirectoryInfo(p);
DirectoryInfo[] subDirs = di.GetDirectories();
if (subDirs.Length > 0)
{
Console.WriteLine("Directories:");
foreach (DirectoryInfo subDir in subDirs)
{
Console.WriteLine(" " + subDir.Name);
}
}
break;
case "rd":
Console.WriteLine("enter compltete path of directory to delete");
p = Console.ReadLine();
DirectoryInfo din = new DirectoryInfo(p);
if (din.Exists)
{
Directory.Delete(p);
if (Directory.Exists(p) == false)
Console.WriteLine("Directory deleted...");
}
else
Console.WriteLine("Directory " +p+" does not yet exist!");
break;
case "del":
Console.WriteLine("enter compltete path of file to delete");
p = Console.ReadLine();
FileInfo fi = new FileInfo(p);
if (fi.Exists)
{
File.Delete(p);
if (File.Exists(p) == false)
Console.WriteLine("File deleted...");
}
else
Console.WriteLine("File "+p+" does not yet exist!");
break;
case "ren":
Console.WriteLine("enter compltete path of file to rename");
p = Console.ReadLine();
FileInfo f = new FileInfo(p);
if (f.Exists)
{
Console.WriteLine("Please enter a new name and complete path for this file:");
string newFilename = Console.ReadLine();
if (newFilename != String.Empty)
{
f.MoveTo(newFilename);
Console.WriteLine("File has been renamed");
}
}
break;
case "md":
Console.WriteLine("enter path");
p = Console.ReadLine();
DirectoryInfo ndi = new DirectoryInfo(p);
Console.WriteLine("Please enter a name for the new directory:");
string newDirName = Console.ReadLine();
if (newDirName != String.Empty)
{
Directory.CreateDirectory(newDirName);
Console.WriteLine("The directory was created!");
}
break;
case "copy":
break;
default:
Console.WriteLine("Invalid Command");
break;
}
}
}
converting switch to if-else is easy (although somewhat redundant i think...). Just take out the switch(copy) part at the top and replace
case "md":
//code here
break;
case "copy":
//code here
break;
with
if(code=="md")
{
//code here
}
else if(code == "copy")
{
//code here
}
etc etc :)
Also, please use [code]
[/code] tags when posting code. As you can see from my code it is much easier to read when correctly formatted
can u make it a lil simpler 4 me,,,by writing a lil code as i told u im a beginner!!! there are some points :
-where to write /do i have to write a switch in it.
-code word in IF statment, where do ihv to initialize it .
the 'code' variable was taken from your code. If you dont know what your code is doing then you need to figure that out before you try and adapt it.
I've given you the syntax for the if-else block and told you which parts it replaces. You need to do some of the work yourself if you are going to learn anything.
I'm busy over the weekend so have a go, play around with the code and try to figure it out. If noone else chimes in i'll check in on the thread in a couple of days.
thnx for dat much help !!!!!ihv got examz ,,,after dat im gonna work on it after all datz my assignment ,,,thing is i dnt wanna learn C# cz datz not my shelly,just gonna go thru with it ,,,,im gud in JAVA n i wanna keep dat up,,,,bt still thnx ,when ill try ill tell u abt dat ,,,k ,,,,,,bt if i didnt get thru it ,,u hv 2 help me with it K!!!!!!!!
Have a look.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace dos_commands
{
class DosApp
{
string curPath;
bool isExitEntered;
string input;
public DosApp()
{
this.curPath = "D:\\";
this.isExitEntered = false;
}
public void Start()
{
Console.WriteLine("Welcome to Dos ");
while (!this.isExitEntered)
{
Console.Write(this.curPath + ">");
this.input = Console.ReadLine();
string[] tokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
ExecuteCommand(tokens);
}
}
void ExecuteCommand(string[] tokens)
{
string p;
if (tokens.Length > 0)
{
string code = tokens[0].ToLower();
switch (code)
{
case "exit":
this.isExitEntered = true;
break;
case "dir":
Console.WriteLine("enter path");
p = Console.ReadLine();
DirectoryInfo di = new DirectoryInfo(p);
DirectoryInfo[] subDirs = di.GetDirectories();
if (subDirs.Length > 0)
{
Console.WriteLine("Directories:");
foreach (DirectoryInfo subDir in subDirs)
{
Console.WriteLine(" " + subDir.Name);
}
}
break;
case "rd":
Console.WriteLine("enter compltete path of directory to delete");
p = Console.ReadLine();
DirectoryInfo din = new DirectoryInfo(p);
if (din.Exists)
{
Directory.Delete(p);
if (Directory.Exists(p) == false)
Console.WriteLine("Directory deleted...");
}
else
Console.WriteLine("Directory " + p + " does not yet exist!");
break;
case "del":
Console.WriteLine("enter compltete path of file to delete");
p = Console.ReadLine();
FileInfo fi = new FileInfo(p);
if (fi.Exists)
{
File.Delete(p);
if (File.Exists(p) == false)
Console.WriteLine("File deleted...");
}
else
Console.WriteLine("File " + p + " does not yet exist!");
break;
case "ren":
Console.WriteLine("enter compltete path of file to rename");
p = Console.ReadLine();
FileInfo f = new FileInfo(p);
if (f.Exists)
{
Console.WriteLine("Please enter a new name and complete path for this file:");
string newFilename = Console.ReadLine();
if (newFilename != String.Empty)
{
f.MoveTo(newFilename);
Console.WriteLine("File has been renamed");
}
}
break;
case "md":
Console.WriteLine("enter path");
p = Console.ReadLine();
DirectoryInfo ndi = new DirectoryInfo(p);
Console.WriteLine("Please enter a name for the new directory:");
string newDirName = Console.ReadLine();
if (newDirName != String.Empty)
{
Directory.CreateDirectory(newDirName);
Console.WriteLine("The directory was created!");
}
break;
case "copy":
break;
default:
Console.WriteLine("Invalid Command");
break;
}
}
}
}
}
class MainApp
{
static void Main()
{
new dos_commands.DosApp().Start();
}
}
thnx it run,,i know
but can u plz tell me how to do it with IF ELSE statment (BRIEFLY)!!!!
plz ,,thnx!!!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.