C#隐藏控制台键盘输入的方法
作者:work24 时间:2022-04-29 21:11:06
本文实例讲述了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#程序设计有所帮助。
标签:C#,控制台,键盘
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android编程中避免内存泄露的方法总结
2023-07-27 19:32:50
Spring boot配置绑定和配置属性校验的方式详解
2022-04-21 03:06:06
![](https://img.aspxhome.com/file/2023/4/64884_0s.png)
Spring如何消除代码中的if-else/switch-case
2021-12-12 03:04:47
java 请求跨域问题解决方法实例详解
2023-08-24 02:55:11
快速学习六大排序算法
2023-11-02 22:36:19
![](https://img.aspxhome.com/file/2023/5/105175_0s.gif)
详解Java泛型及其应用
2023-09-21 22:38:32
Java读取并下载网络文件的方法
2023-03-18 11:47:05
Java 高并发三:Java内存模型和线程安全详解
2021-10-24 07:04:13
![](https://img.aspxhome.com/file/2023/6/59966_0s.png)
DevExpress中GridControl列转义的实现方法
2022-11-10 05:14:31
![](https://img.aspxhome.com/file/2023/8/68078_0s.png)
使用Java实现qq邮箱发送邮件
2023-10-14 18:24:36
![](https://img.aspxhome.com/file/2023/1/58561_0s.jpg)
java + dom4j.jar提取xml文档内容
2023-11-29 03:55:10
Mybatis Plus中的流式查询案例
2023-08-18 16:35:13
IDEA安装阿里巴巴编码规范插件的两种方式详解(在线安装和离线安装)
2022-07-23 19:18:54
![](https://img.aspxhome.com/file/2023/9/60379_0s.png)
C++实现leetcode(3.最长无重复字符的子串)
2023-06-25 03:17:22
聊聊@RequestBody和Json之间的关系
2023-11-27 03:31:45
java9版本特性资源自动关闭的语法增强
2023-10-30 23:35:24
![](https://img.aspxhome.com/file/2023/9/63449_0s.png)
Struts 2中实现Ajax的三种方式
2022-04-30 05:46:28
![](https://img.aspxhome.com/file/2023/7/65307_0s.jpg)
jenkins安装及其配置笔记
2022-10-03 11:01:19
![](https://img.aspxhome.com/file/2023/8/64048_0s.png)
VScode 打造完美java开发环境最新教程
2023-02-24 16:02:10
浅谈Java 继承接口同名函数问题
2023-07-22 13:28:47
![](https://img.aspxhome.com/file/2023/0/57720_0s.jpg)