943,165 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 837
  • C# RSS
Mar 11th, 2010
0

how Console app acts as a DOS shell

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
attifa tariq is offline Offline
5 posts
since Mar 2010
Mar 11th, 2010
2
Re: how Console app acts as a DOS shell
>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.ph...niweb_policies
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,525 posts
since Oct 2008
Mar 12th, 2010
0
Re: how Console app acts as a DOS shell
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

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace dos_commands
  8. {
  9. class DosApp
  10. {
  11. string curPath;
  12. bool isExitEntered;
  13. string input;
  14.  
  15. public DosApp()
  16. {
  17. this.curPath = "D:\\";
  18. this.isExitEntered = false;
  19. }
  20.  
  21. public void Start()
  22. {
  23. Console.WriteLine("Welcome to Dos ");
  24.  
  25. while (!this.isExitEntered)
  26. {
  27. Console.Write(this.curPath + ">");
  28. this.input = Console.ReadLine();
  29. string[] tokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  30. ExecuteCommand(tokens);
  31. }
  32.  
  33.  
  34. }
  35.  
  36.  
  37. void ExecuteCommand(string[] tokens)
  38. {
  39.  
  40. string p;
  41. if (tokens.Length > 0)
  42. {
  43. string code = tokens[0].ToLower();
  44.  
  45. switch (code)
  46. {
  47. case "exit":
  48. this.isExitEntered = true;
  49. break;
  50.  
  51. case "dir":
  52. Console.WriteLine("enter path");
  53. p = Console.ReadLine();
  54. DirectoryInfo di = new DirectoryInfo(p);
  55. DirectoryInfo[] subDirs = di.GetDirectories();
  56. if (subDirs.Length > 0)
  57. {
  58. Console.WriteLine("Directories:");
  59. foreach (DirectoryInfo subDir in subDirs)
  60. {
  61. Console.WriteLine(" " + subDir.Name);
  62. }
  63. }
  64. break;
  65.  
  66. case "rd":
  67. Console.WriteLine("enter compltete path of directory to delete");
  68. p = Console.ReadLine();
  69.  
  70. DirectoryInfo din = new DirectoryInfo(p);
  71.  
  72. if (din.Exists)
  73. {
  74. Directory.Delete(p);
  75. if (Directory.Exists(p) == false)
  76. Console.WriteLine("Directory deleted...");
  77. }
  78. else
  79. Console.WriteLine("Directory " +p+" does not yet exist!");
  80.  
  81. break;
  82. case "del":
  83. Console.WriteLine("enter compltete path of file to delete");
  84. p = Console.ReadLine();
  85.  
  86. FileInfo fi = new FileInfo(p);
  87.  
  88. if (fi.Exists)
  89. {
  90. File.Delete(p);
  91. if (File.Exists(p) == false)
  92. Console.WriteLine("File deleted...");
  93. }
  94. else
  95. Console.WriteLine("File "+p+" does not yet exist!");
  96.  
  97. break;
  98. case "ren":
  99. Console.WriteLine("enter compltete path of file to rename");
  100. p = Console.ReadLine();
  101. FileInfo f = new FileInfo(p);
  102. if (f.Exists)
  103. {
  104. Console.WriteLine("Please enter a new name and complete path for this file:");
  105. string newFilename = Console.ReadLine();
  106. if (newFilename != String.Empty)
  107. {
  108. f.MoveTo(newFilename);
  109. Console.WriteLine("File has been renamed");
  110. }
  111. }
  112.  
  113. break;
  114. case "md":
  115.  
  116. Console.WriteLine("enter path");
  117. p = Console.ReadLine();
  118. DirectoryInfo ndi = new DirectoryInfo(p);
  119. Console.WriteLine("Please enter a name for the new directory:");
  120. string newDirName = Console.ReadLine();
  121. if (newDirName != String.Empty)
  122. {
  123.  
  124. Directory.CreateDirectory(newDirName);
  125.  
  126. Console.WriteLine("The directory was created!");
  127.  
  128.  
  129. }
  130.  
  131. break;
  132. case "copy":
  133.  
  134. break;
  135.  
  136.  
  137. default:
  138. Console.WriteLine("Invalid Command");
  139. break;
  140.  
  141.  
  142. }
  143.  
  144. }
  145. }
Last edited by adatapost; Mar 12th, 2010 at 10:12 pm. Reason: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
attifa tariq is offline Offline
5 posts
since Mar 2010
Mar 12th, 2010
0
Re: how Console app acts as a DOS shell
converting switch to if-else is easy (although somewhat redundant i think...). Just take out the switch(copy) part at the top and replace
C# Syntax (Toggle Plain Text)
  1. case "md":
  2. //code here
  3. break;
  4.  
  5. case "copy":
  6. //code here
  7. break;
with
C# Syntax (Toggle Plain Text)
  1. if(code=="md")
  2. {
  3. //code here
  4. }
  5. else if(code == "copy")
  6. {
  7. //code here
  8. }

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
Last edited by Ryshad; Mar 12th, 2010 at 12:38 pm.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Mar 12th, 2010
0

code thru which console app acts like a dos shell

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 .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
attifa tariq is offline Offline
5 posts
since Mar 2010
Mar 12th, 2010
0
Re: how Console app acts as a DOS shell
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.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Mar 13th, 2010
-1
Re: how Console app acts as a DOS shell
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!!!!!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
attifa tariq is offline Offline
5 posts
since Mar 2010
Mar 13th, 2010
0
Re: how Console app acts as a DOS shell
Have a look.

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace dos_commands
  8. {
  9. class DosApp
  10. {
  11. string curPath;
  12. bool isExitEntered;
  13. string input;
  14.  
  15. public DosApp()
  16. {
  17. this.curPath = "D:\\";
  18. this.isExitEntered = false;
  19. }
  20.  
  21. public void Start()
  22. {
  23. Console.WriteLine("Welcome to Dos ");
  24.  
  25. while (!this.isExitEntered)
  26. {
  27. Console.Write(this.curPath + ">");
  28. this.input = Console.ReadLine();
  29. string[] tokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  30. ExecuteCommand(tokens);
  31. }
  32.  
  33.  
  34. }
  35.  
  36.  
  37. void ExecuteCommand(string[] tokens)
  38. {
  39.  
  40. string p;
  41. if (tokens.Length > 0)
  42. {
  43. string code = tokens[0].ToLower();
  44.  
  45. switch (code)
  46. {
  47. case "exit":
  48. this.isExitEntered = true;
  49. break;
  50.  
  51. case "dir":
  52. Console.WriteLine("enter path");
  53. p = Console.ReadLine();
  54. DirectoryInfo di = new DirectoryInfo(p);
  55. DirectoryInfo[] subDirs = di.GetDirectories();
  56. if (subDirs.Length > 0)
  57. {
  58. Console.WriteLine("Directories:");
  59. foreach (DirectoryInfo subDir in subDirs)
  60. {
  61. Console.WriteLine(" " + subDir.Name);
  62. }
  63. }
  64. break;
  65.  
  66. case "rd":
  67. Console.WriteLine("enter compltete path of directory to delete");
  68. p = Console.ReadLine();
  69.  
  70. DirectoryInfo din = new DirectoryInfo(p);
  71.  
  72. if (din.Exists)
  73. {
  74. Directory.Delete(p);
  75. if (Directory.Exists(p) == false)
  76. Console.WriteLine("Directory deleted...");
  77. }
  78. else
  79. Console.WriteLine("Directory " + p + " does not yet exist!");
  80.  
  81. break;
  82. case "del":
  83. Console.WriteLine("enter compltete path of file to delete");
  84. p = Console.ReadLine();
  85.  
  86. FileInfo fi = new FileInfo(p);
  87.  
  88. if (fi.Exists)
  89. {
  90. File.Delete(p);
  91. if (File.Exists(p) == false)
  92. Console.WriteLine("File deleted...");
  93. }
  94. else
  95. Console.WriteLine("File " + p + " does not yet exist!");
  96.  
  97. break;
  98. case "ren":
  99. Console.WriteLine("enter compltete path of file to rename");
  100. p = Console.ReadLine();
  101. FileInfo f = new FileInfo(p);
  102. if (f.Exists)
  103. {
  104. Console.WriteLine("Please enter a new name and complete path for this file:");
  105. string newFilename = Console.ReadLine();
  106. if (newFilename != String.Empty)
  107. {
  108. f.MoveTo(newFilename);
  109. Console.WriteLine("File has been renamed");
  110. }
  111. }
  112.  
  113. break;
  114. case "md":
  115.  
  116. Console.WriteLine("enter path");
  117. p = Console.ReadLine();
  118. DirectoryInfo ndi = new DirectoryInfo(p);
  119. Console.WriteLine("Please enter a name for the new directory:");
  120. string newDirName = Console.ReadLine();
  121. if (newDirName != String.Empty)
  122. {
  123.  
  124. Directory.CreateDirectory(newDirName);
  125.  
  126. Console.WriteLine("The directory was created!");
  127.  
  128.  
  129. }
  130.  
  131. break;
  132. case "copy":
  133.  
  134. break;
  135.  
  136.  
  137. default:
  138. Console.WriteLine("Invalid Command");
  139. break;
  140.  
  141.  
  142. }
  143.  
  144. }
  145. }
  146. }
  147. }
  148. class MainApp
  149. {
  150. static void Main()
  151. {
  152. new dos_commands.DosApp().Start();
  153. }
  154. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,525 posts
since Oct 2008
Mar 13th, 2010
0
Re: how Console app acts as a DOS shell
thnx it run,,i know
but can u plz tell me how to do it with IF ELSE statment (BRIEFLY)!!!!
plz ,,thnx!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
attifa tariq is offline Offline
5 posts
since Mar 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Adding data from datagridview to another datagridview
Next Thread in C# Forum Timeline: Consecutive Exam Problem GA





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC