【问题标题】:In Ubuntu, how can I clear console history with C#?在 Ubuntu 中,如何使用 C# 清除控制台历史记录?
【发布时间】:2021-10-05 12:32:16
【问题描述】:

我是 C# 的新手。我正在创建一个简单的控制台应用程序。我的任务是在控制台的某个地方做一个标记。并每次删除旧位置。有用。但有一个问题。当使用console.clear();只需转移到新框架即可查看我之前标记点的旧位置。

每次点击后都会发生滚动

如何删除以前的控制台值?

我的代码:

using System;
    
    namespace paint
    {
        class Point
        {
            public int x {get;set;}
            public int y {get;set;}
    //public int mark_y {get;set;}
    //public int mark_x {get;set;}

            protected int[] Position(){
                this.x = x;
                this.y = y;
                //this.mark_x = mark_x;
                //this.mark_y = mark_y;
                return new int [] {this.x,this.y};
            }
        }
        class Program 
        {
            static void Main(string[] args)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Point position = new Point{x = 10, y = 5};
                do
                {
                    Console.SetCursorPosition(position.x, position.y);
                    System.ConsoleKeyInfo console_key = Console.ReadKey(true);
                    if(console_key.Key == ConsoleKey.DownArrow && position.y >= 0 && position.y <= 9){
                        position.y += 1;
                    }
                    if(console_key.Key == ConsoleKey.UpArrow && position.y <= 10 && position.y >= 2){
                        position.y -= 1;
                    }
                    if(console_key.Key == ConsoleKey.LeftArrow && position.x <= 19 && position.x >= 1){
                        position.x -= 1;
                    }
                    if(console_key.Key == ConsoleKey.RightArrow && position.x >= 0 && position.x <= 18){
                        position.x += 1;
                    }
                    if(console_key.Key == ConsoleKey.Spacebar){
                        //position.mark_x = position.x;
                        //position.mark_y = position.y;
                        Console.Clear();
                        Console.SetCursorPosition(position.x, position.y);
                        Console.WriteLine("█");
                    }
                } while (true);
            }
        }
    }

【问题讨论】:

标签: c# ubuntu console console-application


【解决方案1】:

您可以通过在每次使用Console Functions 重置位置时创建一个新控制台来执行此操作。使用FreeConsole 分离当前控制台,然后使用AllocConsole 创建一个新控制台。

【讨论】:

  • 在 ubuntu 上不起作用
  • "仅在 'windows' 上受支持。-我的错误
  • 哦,这就是为什么首先发生这种情况。我运行了您的代码,它在我的 Windows 机器上按预期工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 2011-11-16
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多