Simulate a Console.ReadLine()

本文关键字:ReadLine Console Simulate | 更新日期: 2023-09-27 18:36:16

我正在做一个自定义CMD并尝试做一个"exec"函数来执行脚本文件中的大量命令,但问题是基本上它是由ReadLine触发的,所以我会做一个exec直接执行命令,就像如果这是一个ReadLine一样。

当前代码:

 string[] command = Console.ReadLine().Split(' ');
 switch(command[0])
 {
 // Example
 case "createdir":
     if(command.Length > 0)
     {
try
{
       Directory.Create(command.ToString().Replace("createdir ", string.Empty);
}
               // Catch all exceptions.
               catch (IOException ioe)
                {
                    Console.Error.WriteLine("$error->" + ioe.ToString());
                }
                catch (UnauthorizedAccessException uae)
                {
                    Console.Error.WriteLine("$error->" + uae.ToString());
                }
                catch(ArgumentNullException ane)
                {
                    Console.Error.WriteLine("$error->" + ane.ToString());
                }
     }
     else
     {
     try
                {
                    Directory.CreateDirectory(directory_loc);
                    Console.WriteLine("Directory created!");
                }
                // Catch all exceptions
                catch (IOException ioe)
                {
                    Console.Error.WriteLine("$error->" + ioe.ToString());
                }
                catch (UnauthorizedAccessException uae)
                {
                    Console.Error.WriteLine("$error->" + uae.ToString());
                }
                catch(ArgumentNullException ane)
                {
                    Console.Error.WriteLine("$error->" + ane.ToString());
                }
     }
 }

编辑:我正在做一个命令行控制台应用程序,它可以创建目录、创建脚本文件、执行脚本文件、获取计算机/网络信息并在执行命令时遇到问题,命令由在 Console.ReadLine() 创建的字符串 [] 触发

Simulate a Console.ReadLine()

我不太确定我是否理解您要做什么,但我知道 Console.ReadLine 返回一个字符串。有了这些知识,您就可以传入通常与阅读行关联的字符串。例如

public void Main(){
  DoCommand("My executing string");
}
public void DoCommand(string commandLine){
 string[] command = commandLine.split('');
 switch(command){
   // Command List
 }
}