Advertisement
tommarek_CZE

Simple CLI C# File Manager

May 5th, 2024 (edited)
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.73 KB | None | 0 0
  1. using System.Runtime.InteropServices;
  2. using Microsoft.VisualBasic;
  3. using System.Diagnostics;
  4.  
  5. internal class Program
  6. {
  7.     private static void Main(string[] args)
  8.     {
  9.         // Set current directory value to default
  10.         string currentDir = @"C:\\";
  11.  
  12.         Console.WriteLine("CLI File Manager V1");
  13.         Console.WriteLine("Enter 'help' command to get help");
  14.  
  15.         // Function that open file with default program
  16.         void OpenWithDefaultProgram(string path)
  17.         {   try {
  18.                 // Create new procces, assing explorer as procces and then starts the procces
  19.                 using Process fileopener = new Process();
  20.  
  21.                 fileopener.StartInfo.FileName = "explorer";
  22.                 fileopener.StartInfo.Arguments = "\"" + path + "\"";
  23.                 fileopener.Start();
  24.             } catch {
  25.                 Console.WriteLine("Exception occured while executing command");
  26.             }
  27.         }
  28.  
  29.         // Function that lists all directories and files
  30.         void listCommand() {
  31.             try {
  32.                 Console.WriteLine("Showing contents of " + currentDir);
  33.                 // For each file, get its name and displays it
  34.                 string[] allfiles = Directory.GetFiles(currentDir);
  35.                 foreach (var file in allfiles){
  36.                     FileInfo info = new FileInfo(file);
  37.                     Console.WriteLine(info.Name + " <FILE>");
  38.                 }
  39.  
  40.                 // For each directory, get its name and displays it
  41.                 string[] directories = Directory.GetDirectories(currentDir);
  42.                 foreach (string dir in directories)
  43.                 {
  44.                     string trimmedString = Path.GetFileName(dir);
  45.  
  46.                     Console.WriteLine(trimmedString + " <DIRECTORY>");
  47.                 }
  48.             } catch {
  49.                 Console.WriteLine("Exception occured while executing command");
  50.             }
  51.         }
  52.  
  53.         void goCommand() {
  54.             try {
  55.                 Console.WriteLine("Enter directory name");
  56.                 // Read from the console what directory user wanna acces
  57.                 string arg = Console.ReadLine();
  58.                 // 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
  59.                 string[] directories = Directory.GetDirectories(currentDir);
  60.                 foreach (string dir in directories)
  61.                 {
  62.                     string trimmedString = Path.GetFileName(dir);
  63.                     if (trimmedString == arg) {
  64.                         currentDir = dir;
  65.                         Console.WriteLine("Jumped into " + trimmedString + " directory");
  66.                         return;
  67.                     }
  68.                 }
  69.                 Console.WriteLine("Directory " + arg + " was not founded");
  70.             } catch {
  71.                 Console.WriteLine("Exception occured while executing command");
  72.             }
  73.         }
  74.  
  75.         void returnCommand() {
  76.             try {
  77.                 // Get parent of current directory and then sets it as current directory
  78.                 string parentDirectory = Directory.GetParent(currentDir).FullName;
  79.                 currentDir = parentDirectory;
  80.                 Console.WriteLine("Jumped into " + currentDir + " directory");
  81.             } catch {
  82.                 Console.WriteLine("Exception occured while executing command");
  83.             }
  84.         }
  85.  
  86.         void openCommand() {
  87.             try {
  88.                 Console.WriteLine("Enter file name");
  89.                 // Get arguments from console, what file the user wanna open
  90.                 string arg = Console.ReadLine();
  91.                 // Gets all files and scans them for desired file, if found it would be opened via OpenWithDefaultProgram function
  92.                 string[] allfiles = Directory.GetFiles(currentDir);
  93.                 foreach (var file in allfiles){
  94.                     FileInfo info = new FileInfo(file);
  95.                     if (arg == info.Name) {
  96.                         OpenWithDefaultProgram(info.FullName);
  97.                         Console.WriteLine("File " + info.Name + " was opened");
  98.                         return;
  99.                     }
  100.                 }
  101.                 Console.WriteLine("File " + arg + " was not founded");
  102.             } catch {
  103.                 Console.WriteLine("Exception occured while executing command");
  104.             }
  105.  
  106.         }
  107.  
  108.         void dcreateCommand() {
  109.             try {
  110.                 Console.WriteLine("Enter directory name that you wanna create");
  111.                 // Gets arguments from console, what directory user wanna create
  112.                 string arg = Console.ReadLine();
  113.                 string path = currentDir + "\\" + arg;
  114.                 // Check if directory does not exist, if donsent exist it would create the desired directory
  115.                 if (!Directory.Exists(path))
  116.                 {
  117.                     Directory.CreateDirectory(path);
  118.                     Console.WriteLine("Directory " + arg + " created succefuely");
  119.                     return;
  120.                 }
  121.                 Console.WriteLine("Directory " + arg + " already exist");
  122.             } catch {
  123.                 Console.WriteLine("Exception occured while executing command");
  124.             }
  125.         }
  126.  
  127.         void fcreateCommand() {
  128.             try {
  129.                 // Gets arguments from console, what file user wanna create
  130.                 Console.WriteLine("Enter file name that you wanna create");
  131.                 string arg = Console.ReadLine();
  132.                 string path = currentDir + "\\" + arg;
  133.  
  134.                 // Check if file does not exist, if donsent exist it would create the desired file
  135.                 if (!File.Exists(path))
  136.                 {
  137.                     File.Create(path).Dispose();
  138.                     Console.WriteLine("File " + arg + " created succefuely");
  139.                     return;      
  140.                 }
  141.                 Console.WriteLine("File " + arg + " already exist");
  142.             } catch {
  143.                 Console.WriteLine("Exception occured while executing command");
  144.             }
  145.         }
  146.  
  147.         void deleteCommand() {
  148.             try {
  149.                 Console.WriteLine("Enter file or directory that you wanna delete");
  150.                 // Get the arguments from console, what folder or file user wanna get deleted
  151.                 string arg = Console.ReadLine();
  152.                 string path = currentDir + "\\" + arg;
  153.                 // Check if directory exist, if yes it would delete the directory
  154.                 if (Directory.Exists(path))
  155.                 {
  156.                     Directory.Delete(path, true);
  157.                     Console.WriteLine("Directory " + arg + "succesfully deleted");
  158.                     return;
  159.                 }
  160.                 // Check if file exist, if yes delete the file
  161.                 else if (File.Exists(path))
  162.                 {
  163.                     File.Delete(path);
  164.                     Console.WriteLine("File " + arg + "succesfully deleted");
  165.                     return;
  166.                 }
  167.                 Console.WriteLine("File or directory " + arg + "dosen't exist");            
  168.             } catch {
  169.                 Console.WriteLine("Exception occured while executing command");
  170.             }
  171.         }
  172.  
  173.         // Display help menu to the console
  174.         void helpCommand() {
  175.             Console.WriteLine("Commands list:");
  176.             Console.WriteLine("list > display contents of current directory");
  177.             Console.WriteLine("go > go to specific directory");
  178.             Console.WriteLine("retirn > go to the parent directory");
  179.             Console.WriteLine("open > open specific file");
  180.             Console.WriteLine("dcreate > create folder in current directory");
  181.             Console.WriteLine("fcreate > create file in current directory");
  182.             Console.WriteLine("delete > delete specific file or directory");
  183.             Console.WriteLine("help > show this current message");
  184.             Console.WriteLine("-----\nDon't put arguments behind commands, the argument handler is after you execute the command");
  185.         }
  186.  
  187.         // Switch that binds functions to commands
  188.         void handleCommands(string command)
  189.         {
  190.             switch (command)
  191.             {
  192.                 case "list": // List files/Directories
  193.                     listCommand();
  194.                     break;
  195.                 case "dcreate": // Create Folder
  196.                     dcreateCommand();
  197.                     break;
  198.                 case "open": // Open File
  199.                     openCommand();
  200.                     break;
  201.                 case "go": // Go to directory/path
  202.                     goCommand();
  203.                     break;
  204.                 case "return": // Return to the lower directory
  205.                     returnCommand();
  206.                     break;
  207.                 case "fcreate": // Create file
  208.                     fcreateCommand();
  209.                     break;
  210.                 case "help": // Show help dialog
  211.                     helpCommand();
  212.                     break;
  213.                 case "delete": // Delete file
  214.                     deleteCommand();
  215.                     break;
  216.                 default:
  217.                     Console.WriteLine("Undefined command");
  218.                     break;
  219.             }
  220.         }
  221.        
  222.         // Handle console input, and make infinite loop
  223.         void handeInput()
  224.         {
  225.             Console.WriteLine("          ");
  226.             string command = Console.ReadLine();
  227.             Console.WriteLine("Command executed> " + command);
  228.             handleCommands(command);
  229.             handeInput();
  230.         }
  231.  
  232.         handeInput();
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement