using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] parsed = { "", "", "", "", "" };
String currentdir = "C:\";
String command = "";
Console.WriteLine("Teh 2010 DOS version - By Mohammad Osman @ software -group 2\n");
while (command != "exit" && command != "quit")
{
for (int clear = 0; clear < parsed.Length; clear++)
{
// Have to clear the array so it doesnt have old parameters inside
parsed[clear] = null;
}
Thread.Sleep(100);
Console.Write(currentdir.ToUpper());
command = Console.ReadLine();
if (command.Contains(" "))
{
parsed = command.Split(' ');
command = parsed[0];
currentdir = sendcommand1(parsed[0], parsed, currentdir);
}
else
{
parsed[0] = command;
parsed[1] = "0";
currentdir = sendcommand1(command, parsed, currentdir);
}
}
}
static String sendcommand1(String commando, string[] arg, String currentdir)
{
String Shortname = LongToShort(arg);
System.IO.Directory.SetCurrentDirectory(currentdir);
commando = commando.ToLower(); // Doesn't matter if user uses upper/lowercase in commands
switch (commando)
{
case "dir":
if (arg[1] == "0") arg[1] = "*.*";
long totalsize = 0;
int numberoffiles = 0;
try
{
System.IO.DirectoryInfo dire = new System.IO.DirectoryInfo(@currentdir);
Console.WriteLine("Listing files in: {0}", currentdir.ToUpper());
Console.WriteLine("Date Time Size File");
foreach (System.IO.DirectoryInfo dirinfo in dire.GetDirectories(arg[1]))
{
String dirname = dirinfo.Name;
String dirdate = dirinfo.CreationTime.ToString().Substring(0, 16);
Console.Write("{0} ", dirdate);
Console.Write(" <DIR> ");
Console.WriteLine(dirname);
}
foreach (System.IO.FileInfo file in dire.GetFiles(arg[1]))
{
numberoffiles++;
long filesize = file.Length;
String filename = file.Name;
String filedate = file.CreationTime.ToString().Substring(0, 16);
int filediff = 10 - filesize.ToString().Length;
totalsize += filesize;
Console.Write("{0} ", filedate);
Console.Write(filesize);
for (int i = 0; i < filediff; i++)
{
Console.Write(" ");
}
Console.Write(filename);
Console.WriteLine();
}
Console.WriteLine("\nNumber of files: {0} - {1} bytes\n", numberoffiles.ToString(), totalsize.ToString());
}
catch (Exception e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
break; //"dir"
case "cd":
String newdir = currentdir + Shortname;
bool direxist = System.IO.Directory.Exists(newdir);
if (direxist)
{
System.IO.Directory.SetCurrentDirectory(newdir);
if (System.IO.Directory.GetCurrentDirectory() != "C:\")
{
currentdir = System.IO.Directory.GetCurrentDirectory() + "\";
}
else
{
currentdir = System.IO.Directory.GetCurrentDirectory();
}
}
else
{
Console.WriteLine("Can't find directory");
}
break;//"cd"
case "cd..":
String newdir2 = currentdir + "..";
bool direxist2 = System.IO.Directory.Exists(newdir2);
if (direxist2)
{
System.IO.Directory.SetCurrentDirectory(newdir2);
if (System.IO.Directory.GetCurrentDirectory() != "C:\")
{
currentdir = System.IO.Directory.GetCurrentDirectory() + "\";
}
else
{
currentdir = System.IO.Directory.GetCurrentDirectory();
}
}
break;//"cd.."
case "type":
string line;
TimeSpan nn = new TimeSpan(DateTime.Now.Ticks);
bool typefileexist = System.IO.File.Exists(currentdir + Shortname);
if (typefileexist)
{
try
{
System.IO.StreamReader typefile = new System.IO.StreamReader(@currentdir + Shortname);
while ((line = typefile.ReadLine()) != null)
{
Console.WriteLine(line.ToString());
}
DateTime after = DateTime.Now.Subtract(nn);
Console.WriteLine("Took {0} seconds to complete\n", after.Second.ToString());
typefile.Close();
}
catch (Exception e)
{
Console.WriteLine("Error accessing file: {0}\n", e.Message.ToString());
}
}
else
{
Console.WriteLine("Couldn't find file");
}
break;//"type"
case "ping":
if (arg[1] == "0") arg[1] = "127.0.0.1";
try
{
String netadd = System.Net.Dns.GetHostEntry(arg[1]).HostName.ToString();
Console.WriteLine("Translated {0} to {1}", arg[1], netadd);
Console.WriteLine();
System.Net.NetworkInformation.Ping netping = new System.Net.NetworkInformation.Ping();
netping.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
string pingdata = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] pingbuffer = Encoding.ASCII.GetBytes(pingdata);
int timeout = 500;
PingOptions pingoptions = new PingOptions(54, true);
AutoResetEvent waiter = new AutoResetEvent(false);
Console.WriteLine("Pinging {0} with 32byte data:\n", arg[1]);
for (int i = 0; i < 4; i++)
{
netping.SendAsync(arg[1], timeout, pingbuffer, pingoptions, waiter);
waiter.WaitOne();
}
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
break;//"ping"
case "ipconfig":
Console.WriteLine("Network information:\n");
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Hostname: {0}", ipProperties.HostName);
Console.WriteLine("Domain: {0}", ipProperties.DomainName);
Console.WriteLine("Loopback address: {0}", System.Net.IPAddress.Loopback.ToString());
try
{
XmlDocument doc = new XmlDocument();
doc.Load("http://www.showmyip.com/xml/");
Console.WriteLine("WAN IP: {0}", doc.SelectSingleNode("//ip_address/ip").FirstChild.Value);
}
catch (Exception e)
{
Console.WriteLine("WAN IP: Couldn't connect to
www.showmyip.com"); }
Console.WriteLine("Adapters:\n");
foreach (NetworkInterface adapter in adapters)
{
if (adapter.Description.ToString() == "MS TCP Loopback interface") break; // Don't wanna see that
Console.WriteLine("------------------------------------------------");
Console.WriteLine("Adapter: {0}", adapter.Description.ToString());
Console.WriteLine("Name: {0}", adapter.Name.ToString());
Console.WriteLine("MAC: {0}", adapter.GetPhysicalAddress().ToString());
foreach (IPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
{
Console.WriteLine("IP: {0}", ip.Address);
}
Console.WriteLine("ID: {0}", adapter.Id.ToString());
Console.WriteLine("Type: {0}", adapter.NetworkInterfaceType.ToString());
Console.WriteLine("Link: {0}", adapter.OperationalStatus.ToString());
Console.WriteLine("Speed: {0}", adapter.Speed / 1000000 + "Mbit");
Console.WriteLine("Support Multicast: {0}", adapter.SupportsMulticast);
foreach (GatewayIPAddressInformation gatewayAddr in adapter.GetIPProperties().GatewayAddresses)
{
Console.WriteLine("Gateway: {0}", gatewayAddr.Address.ToString());
}
foreach (IPAddress address in adapter.GetIPProperties().DnsAddresses)
{
Console.WriteLine("DNS: {0}", address.ToString());
}
Console.WriteLine("------------------------------------------------\n");
}
Console.WriteLine();
break;//"ipconfig"
case "netstat":
Console.WriteLine();
Console.WriteLine("Active TCP Connections:");
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation c in connections)
{
Console.WriteLine("{0} <-> {1} \tState: {2}", c.LocalEndPoint.ToString(), c.RemoteEndPoint.ToString(), c.State.ToString());
}
Console.WriteLine();
break;//"netstat"
case "del":
bool delfileexist = System.IO.File.Exists(currentdir + Shortname);
if (delfileexist)
{
try
{
System.IO.File.Delete(currentdir + Shortname);
Console.WriteLine("File deleted");
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
else
{
Console.WriteLine("Couldn't find file: {0}", currentdir + Shortname);
}
Console.WriteLine();
break;//"del"
case "ren":
try
{
bool renfileexist = System.IO.File.Exists(currentdir + arg[1]);
bool rentargetfileexist = System.IO.File.Exists(currentdir + arg[2]);
if (rentargetfileexist)
{
Console.WriteLine("Targetfile already exists.. aborting\n");
break;
}
if (renfileexist)
{
System.IO.File.Move(currentdir + arg[1], currentdir + arg[2]);
Console.WriteLine("File renamed");
}
else
{
Console.WriteLine("Couldn't find file");
}
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine("Wrong Input");
}
break;//"ren"
case "copy":
bool copyfileexist = System.IO.File.Exists(currentdir + arg[1]);
bool copytargetdirexist = System.IO.Directory.Exists(arg[2]);
if (copyfileexist)
{
if (!copytargetdirexist)
{
Console.WriteLine("Target directory could not be found. aborting.");
break;
}
if (arg.Length.ToString() == "4" && arg[3] == "/y")
{
System.IO.File.Copy(currentdir + arg[1], arg[2] + "\" + arg[1], true);
}
else
{
bool fileexisttarget = System.IO.File.Exists(arg[2] + "\" + arg[1]);
if (fileexisttarget)
{
Console.Write("File already exist in target directory, overwrite? Y/N: ");
ConsoleKeyInfo keyinfo;
keyinfo = Console.ReadKey();
if (keyinfo.KeyChar.ToString().ToLower() == "y")
{
System.IO.File.Copy(currentdir + arg[1], arg[2] + "\" + arg[1], true);
}
else
{
Console.WriteLine("\nFile not copied due to user input N");
break;
}
}
else
{
System.IO.File.Copy(arg[1], arg[2] + "\" + arg[1]);
}
}
}
else
{
Console.WriteLine("Couldn't find file");
}
Console.WriteLine();
break;//"copy"
case "move":
bool movefileexist = System.IO.File.Exists(currentdir + arg[1]);
bool movetargetdirexist = System.IO.Directory.Exists(arg[2]);
if (movefileexist)
{
if (!movetargetdirexist)
{
Console.WriteLine("Target directory could not be found. aborting.");
break;
}
bool mfileexisttarget = System.IO.File.Exists(arg[2] + "\" + arg[1]);
if (mfileexisttarget)
{
Console.Write("File already exist in target directory, overwrite? Y/N: ");
ConsoleKeyInfo keyinfo;
keyinfo = Console.ReadKey();
if (keyinfo.KeyChar.ToString().ToLower() == "y")
{
System.IO.File.Delete(arg[2] + "\" + arg[1]);
System.IO.File.Move(currentdir + arg[1], arg[2] + "\" + arg[1]);
}
else
{
Console.WriteLine("\nFile not copied due to user input N");
break;
}
}
else
{
System.IO.File.Move(arg[1], arg[2] + "\" + arg[1]);
}
}
else
{
Console.WriteLine("Couldn't find file");
}
Console.WriteLine();
break;//"move"
case "md":
bool diralreadyexist = System.IO.Directory.Exists(currentdir + "\" + Shortname);
if (diralreadyexist)
{
Console.WriteLine("Directory already exists, aborting.\n");
break;
}
else
{
// try/catch illegal characters in path hvis " "
System.IO.Directory.CreateDirectory(currentdir + "\" + Shortname);
}
break;//"md"
case "rd":
bool rddirectoryexist = System.IO.Directory.Exists(currentdir + "\" + Shortname);
if (rddirectoryexist)
{
try
{
System.IO.Directory.Delete(currentdir + "\" + Shortname);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
else
{
Console.WriteLine("Directory not found\n");
}
break;//"rd"
case "date":
Console.WriteLine("{0}\n", DateTime.Now.ToString());
break;//"date"
case "echo":
for (int i = 1; i < arg.Length; i++)
{
Console.Write("{0} ", arg[i]);
}
Console.WriteLine();
break;//"echo"
case "proc":
Process[] myProcesses = Process.GetProcesses();
Console.WriteLine("ProcessName: ProcessID: ProcessTitle:\n");
int diff = 0;
String diffbuffer = "";
for (int i = 0; i < myProcesses.Length; i++)
{
string processName = myProcesses[i].ProcessName;
diff = 20 - processName.Length;
for (int i2 = 0; i2 < diff; i2++)
{
diffbuffer += " ";
}
int processid = myProcesses[i].Id;
string processtitle = myProcesses[i].MainWindowTitle;
Console.WriteLine("{0}:{1}{2} {3}", processName, diffbuffer, processid.ToString(), processtitle);
diffbuffer = "";
}
break;//"proc"
case "kill":
try
{
int processID = Int32.Parse(arg[1]);
String name = Process.GetProcessById(processID).ProcessName.ToString();
Console.WriteLine("Killing: {0}", name);
if (Process.GetProcessById(processID).CloseMainWindow())
{
Console.WriteLine("CloseMainWindow");
}
else
{
Process.GetProcessById(processID).Kill();
Console.WriteLine("KILLED!!!!!!!!!!!");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
break;//"kill"
case "run":
try { Process.Start(arg[1], LongToShort2(arg)); }
catch (Exception e) { Console.WriteLine("Error running file [{0}]", e.Message); }
break;//"run"
case "shutdown":
Process.Start("shutdown.exe", "-s -t 0 -f");
break;//"shutdown"
case "cls":
Console.Clear();
Console.WriteLine("Teh 1337 DOS version - By Jess Kjellmann\n");
break;//"cls"
case "help":
Console.WriteLine();
Console.WriteLine("dir [*.*] \t\t\t\t List files in current directory");
Console.WriteLine("cls \t\t\t\t\t Clears screen");
Console.WriteLine("exit \t\t\t\t\t Exit the program");
Console.WriteLine("type [file] \t\t\t\t Show content of the file");
Console.WriteLine("ping xxx.xxx.xxx.xxx \t\t\t Ping a host");
Console.WriteLine("ipconfig \t\t\t\t Show network information");
Console.WriteLine("netstat \t\t\t\t Show active TCP connections");
Console.WriteLine("del [file] \t\t\t\t Delete a file");
Console.WriteLine("run [file] [args] \t\t\t Run a file");
Console.WriteLine("ren [file] [newfile] \t\t\t Rename a file");
Console.WriteLine("copy [file] [destination dir] [/y] \t Copy a file");
Console.WriteLine("move [file] [destination dir] \t\t Move a file");
Console.WriteLine("md [directory] \t\t\t\t Create new directory");
Console.WriteLine("rd [directory] \t\t\t\t Delete directory");
Console.WriteLine("echo \t\t\t\t\t Echo stuff on screen");
Console.WriteLine("date \t\t\t\t\t Show date and time");
Console.WriteLine("proc \t\t\t\t\t Show running processes");
Console.WriteLine("kill [id] \t\t\t\t Kill process by ID");
Console.WriteLine("shutdown \t\t\t\t Shut down the computer");
Console.WriteLine();
break;//"help"
case "exit":
break;//"exit"
case "quit":
break;//"quit"
default:
Console.WriteLine("Unknown Command... Type help for available commands");
break;//"default"
}
return currentdir;
}
private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
((AutoResetEvent)e.UserState).Set();
PingReply reply = e.Reply;
DisplayReply(reply);
}
public static void DisplayReply(PingReply reply)
{
if (reply == null) return;
if (reply.Status == IPStatus.TimedOut)
{
Console.WriteLine("Request timed out!");
}
if (reply.Status == IPStatus.Success)
{
Console.Write("Answer from {0}: size={1} time={2}ms\n", reply.Address, reply.Buffer.Length, reply.RoundtripTime.ToString());
}
}
static String LongToShort(string[] args)
{
String ShortName = "";
for (int i = 1; i < args.Length; i++)
{
ShortName += " " + args[i];
}
return ShortName.Substring(1, ShortName.Length - 1);
}
static String LongToShort2(string[] args2)
{
String ShortName2 = "";
if (args2.Length >= 3)
{
for (int i2 = 2; i2 < args2.Length; i2++)
{
ShortName2 += " " + args2[i2];
}
return ShortName2.Substring(1, ShortName2.Length - 1);
}
else
{
return "";
}
}
}
}