C#隐藏控制台键盘输入的方法
作者:work24 发布时间:2022-04-29 21:11:06
标签:C#,控制台,键盘
本文实例讲述了C#隐藏控制台键盘输入的方法。分享给大家供大家参考。具体如下:
using System;
namespace RobvanderWoude
{
class HideInput
{
static int Main( string[] args )
{
try
{
bool clearscreen = false;
if ( args.Length > 1 )
{
return WriteError( "Too many command line arguments" );
}
if ( args.Length == 1 )
{
switch ( args[0].ToUpper( ) )
{
case "/C":
clearscreen = true;
break;
case "/?":
return WriteError( );
default:
return WriteError( "Invalid command line argument \"" + args[0] + "\"" );
}
}
// Set console foreground color to background color to hide what's being typed
ConsoleColor color = Console.ForegroundColor;
Console.ForegroundColor = Console.BackgroundColor;
// Read 1 line of input from the console
string input = Console.ReadLine( );
// Restore the original console foreground color
Console.ForegroundColor = color;
// Clear the screen id specified on the command line
if ( clearscreen )
{
Console.Clear( );
}
// Display the input - which should be redirected for this program to be of any use
Console.WriteLine( input );
// Returncode 0 for success, or 1 if the input was empty or whitespace only
if ( string.IsNullOrWhiteSpace( input ) )
{
return 1;
}
else
{
return 0;
}
}
catch ( Exception e )
{
return WriteError( e.Message );
}
}
public static int WriteError( string errorMessage = "" )
{
Console.ResetColor( );
if ( string.IsNullOrEmpty( errorMessage ) == false )
{
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errorMessage );
Console.ResetColor( );
}
Console.Error.WriteLine( );
Console.Error.WriteLine( "HideInput, Version 1.10" );
Console.Error.WriteLine( "Batch utility to read 1 line of input while hiding what's being typed, by" );
Console.Error.WriteLine( "temporarily setting the console foreground color equal to its background color" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: FOR /F \"tokens=*\" %%A IN ('" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "HIDEINPUT" );
Console.ResetColor( );
Console.Error.WriteLine( "') DO SET password=%%A" );
Console.Error.Write( " or: FOR /F \"tokens=*\" %%A IN ('" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "HIDEINPUT /C" );
Console.ResetColor( );
Console.Error.WriteLine( "') DO SET password=%%A" );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/C" );
Console.ResetColor( );
Console.Error.WriteLine( " clears the screen to remove what's typed from the screen buffer" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
return 1;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。


猜你喜欢
- 错误示例,同一个类中使用异步方法:package com.xqnode.learning.controller;import com.fas
- 前言最近遇到个小问题,要为几十个文本框添加相同的失去焦点事件,常规的办法是在VS的事件管理器里面添加,但那样太繁琐了,几十个文本框,要加几十
- 异常与错误:异常:在Java中程序的错误主要是语法错误和语义错误,一个程序在编译和运行时出现的错误我们统一称之为异常,它是VM(虚拟机)通知
- fatal error C1003: error count exceeds number; stopping compilation中文对
- 1.查找数据库中表的列名<pre name="code" class="html">St
- 一、题目描述题目:使用ThreadLocal管理一号和二号线程,分别存入100元,在三号线程中使用利用一号和二号的计算结果来算出账户的实际金
- 前言之所以要写这篇关于C#反射的随笔,起因有两个:第一个是自己开发的网站需要用到其次就是没看到这方面比较好的文章。所以下定决心自己写一篇,废
- 本文讲述了Android编程中关于单线程模型的理解与分析。分享给大家供大家参考,具体如下:当一个Android程序启动时,Android系统
- 一、前言最近接到一个任务,需要爬取五级行政区划的所有数据(大概71万条数据在),需要爬取的网站:行政区划 - 行政区划代码查询 发
- 前言Spring Boot常用注解整理提示:以下是本篇文章正文内容,下面案例可供参考一、@SpringBootApplication此注解是
- 来源:https://www.cnblogs.com/dato/p/7027949.html
- 对于网站的安全性,是每个网站开发者和运营者最关心的问题。网站一旦出现漏洞,那势必将造成很大的损失。为了提高网站的安全性,首先网站要防注入,最
- using System.Runtime.InteropServices; using System.Text; publicclass F
- XmlTextWriter类允许你将XML写到一个文件中去。这个类包含了很多方法和属性,使用这些属性和方法可以使你更容易地处理XML。为了使
- 初学者,照着书上的抄袭制作,但已经理解了里面的意思和应用,并且进行了稍微改善和异常捕捉。这里记录下,以防以后用到这方面的知识点。窗体设计:c
- Android的一个核心特性就是一个应用程序可作为其他应用程序中的元素,可为其他应用程序提供数据。例如,如果程序需要用某些控件来加载一些图片
- 本文实例为大家分享了java字符串和数字转换工具的具体代码,供大家参考,具体内容如下package com.test.util;/** *
- 本文实例总结了Java中泛型的用法。分享给大家供大家参考。具体如下:1 基本使用public interface List<E>
- class ftp{ private string host = null; &n
- Java 方法执行时的动态分派和静态分派是 Java 实现多态的本质背景Java 的动态分派和静态分派也是 Java 方法的执行原理。 Ja