Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Runtime.InteropServices;
- using Microsoft.VisualBasic;
- using System.Diagnostics;
- internal class Program
- {
- private static void Main(string[] args)
- {
- // Set current directory value to default
- string currentDir = @"C:\\";
- Console.WriteLine("CLI File Manager V1");
- Console.WriteLine("Enter 'help' command to get help");
- // Function that open file with default program
- void OpenWithDefaultProgram(string path)
- { try {
- // Create new procces, assing explorer as procces and then starts the procces
- using Process fileopener = new Process();
- fileopener.StartInfo.FileName = "explorer";
- fileopener.StartInfo.Arguments = "\"" + path + "\"";
- fileopener.Start();
- } catch {
- Console.WriteLine("Exception occured while executing command");
- }
- }
- // Function that lists all directories and files
- void listCommand() {
- try {
- Console.WriteLine("Showing contents of " + currentDir);
- // For each file, get its name and displays it
- string[] allfiles = Directory.GetFiles(currentDir);
- foreach (var file in allfiles){
- FileInfo info = new FileInfo(file);
- Console.WriteLine(info.Name + " <FILE>");
- }
- // For each directory, get its name and displays it
- string[] directories = Directory.GetDirectories(currentDir);
- foreach (string dir in directories)
- {
- string trimmedString = Path.GetFileName(dir);
- Console.WriteLine(trimmedString + " <DIRECTORY>");
- }
- } catch {
- Console.WriteLine("Exception occured while executing command");
- }
- }
- void goCommand() {
- try {
- Console.WriteLine("Enter directory name");
- // Read from the console what directory user wanna acces
- string arg = Console.ReadLine();
- // Gets directories inside current directory and scans the list it for the desired directory, if found the current directory would be assigned to desired directory
- string[] directories = Directory.GetDirectories(currentDir);
- foreach (string dir in directories)
- {
- string trimmedString = Path.GetFileName(dir);
- if (trimmedString == arg) {
- currentDir = dir;
- Console.WriteLine("Jumped into " + trimmedString + " directory");
- return;
- }
- }
- Console.WriteLine("Directory " + arg + " was not founded");
- } catch {
- Console.WriteLine("Exception occured while executing command");
- }
- }
- void returnCommand() {
- try {
- // Get parent of current directory and then sets it as current directory
- string parentDirectory = Directory.GetParent(currentDir).FullName;
- currentDir = parentDirectory;
- Console.WriteLine("Jumped into " + currentDir + " directory");
- } catch {
- Console.WriteLine("Exception occured while executing command");
- }
- }
- void openCommand() {
- try {
- Console.WriteLine("Enter file name");
- // Get arguments from console, what file the user wanna open
- string arg = Console.ReadLine();
- // Gets all files and scans them for desired file, if found it would be opened via OpenWithDefaultProgram function
- string[] allfiles = Directory.GetFiles(currentDir);
- foreach (var file in allfiles){
- FileInfo info = new FileInfo(file);
- if (arg == info.Name) {
- OpenWithDefaultProgram(info.FullName);
- Console.WriteLine("File " + info.Name + " was opened");
- return;
- }
- }
- Console.WriteLine("File " + arg + " was not founded");
- } catch {
- Console.WriteLine("Exception occured while executing command");
- }
- }
- void dcreateCommand() {
- try {
- Console.WriteLine("Enter directory name that you wanna create");
- // Gets arguments from console, what directory user wanna create
- string arg = Console.ReadLine();
- string path = currentDir + "\\" + arg;
- // Check if directory does not exist, if donsent exist it would create the desired directory
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- Console.WriteLine("Directory " + arg + " created succefuely");
- return;
- }
- Console.WriteLine("Directory " + arg + " already exist");
- } catch {
- Console.WriteLine("Exception occured while executing command");
- }
- }
- void fcreateCommand() {
- try {
- // Gets arguments from console, what file user wanna create
- Console.WriteLine("Enter file name that you wanna create");
- string arg = Console.ReadLine();
- string path = currentDir + "\\" + arg;
- // Check if file does not exist, if donsent exist it would create the desired file
- if (!File.Exists(path))
- {
- File.Create(path).Dispose();
- Console.WriteLine("File " + arg + " created succefuely");
- return;
- }
- Console.WriteLine("File " + arg + " already exist");
- } catch {
- Console.WriteLine("Exception occured while executing command");
- }
- }
- void deleteCommand() {
- try {
- Console.WriteLine("Enter file or directory that you wanna delete");
- // Get the arguments from console, what folder or file user wanna get deleted
- string arg = Console.ReadLine();
- string path = currentDir + "\\" + arg;
- // Check if directory exist, if yes it would delete the directory
- if (Directory.Exists(path))
- {
- Directory.Delete(path, true);
- Console.WriteLine("Directory " + arg + "succesfully deleted");
- return;
- }
- // Check if file exist, if yes delete the file
- else if (File.Exists(path))
- {
- File.Delete(path);
- Console.WriteLine("File " + arg + "succesfully deleted");
- return;
- }
- Console.WriteLine("File or directory " + arg + "dosen't exist");
- } catch {
- Console.WriteLine("Exception occured while executing command");
- }
- }
- // Display help menu to the console
- void helpCommand() {
- Console.WriteLine("Commands list:");
- Console.WriteLine("list > display contents of current directory");
- Console.WriteLine("go > go to specific directory");
- Console.WriteLine("retirn > go to the parent directory");
- Console.WriteLine("open > open specific file");
- Console.WriteLine("dcreate > create folder in current directory");
- Console.WriteLine("fcreate > create file in current directory");
- Console.WriteLine("delete > delete specific file or directory");
- Console.WriteLine("help > show this current message");
- Console.WriteLine("-----\nDon't put arguments behind commands, the argument handler is after you execute the command");
- }
- // Switch that binds functions to commands
- void handleCommands(string command)
- {
- switch (command)
- {
- case "list": // List files/Directories
- listCommand();
- break;
- case "dcreate": // Create Folder
- dcreateCommand();
- break;
- case "open": // Open File
- openCommand();
- break;
- case "go": // Go to directory/path
- goCommand();
- break;
- case "return": // Return to the lower directory
- returnCommand();
- break;
- case "fcreate": // Create file
- fcreateCommand();
- break;
- case "help": // Show help dialog
- helpCommand();
- break;
- case "delete": // Delete file
- deleteCommand();
- break;
- default:
- Console.WriteLine("Undefined command");
- break;
- }
- }
- // Handle console input, and make infinite loop
- void handeInput()
- {
- Console.WriteLine(" ");
- string command = Console.ReadLine();
- Console.WriteLine("Command executed> " + command);
- handleCommands(command);
- handeInput();
- }
- handeInput();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement