C# 在c中防止用户输入空控制台#
嗨,我想阻止用户在输入字段中输入任何内容 我尝试过使用if-else,但当没有输入时,控制台一直崩溃。(对于用户输入和ldap地址输入==>我希望它显示“未检测到输入”。并允许用户重新输入用户名) 如果我使用C# 在c中防止用户输入空控制台#,c#,visual-studio-2010,console-application,C#,Visual Studio 2010,Console Application,嗨,我想阻止用户在输入字段中输入任何内容 我尝试过使用if-else,但当没有输入时,控制台一直崩溃。(对于用户输入和ldap地址输入==>我希望它显示“未检测到输入”。并允许用户重新输入用户名) 如果我使用(results==“”),我会得到一个错误: 运算符“==”不能应用于类型为的操作数 “System.DirectoryServices.SearchResult”和“string” 我有办法解决这个问题吗?代码如下所示 从第16行开始受影响的代码(对于代码的顶部块) 如果有帮助的话,我已
(results==“”)
,我会得到一个错误:
运算符“==”不能应用于类型为的操作数
“System.DirectoryServices.SearchResult”和“string”
我有办法解决这个问题吗?代码如下所示
从第16行开始受影响的代码(对于代码的顶部块)
如果有帮助的话,我已经在下面附上了完整的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Data.SqlClient;
namespace ConsoleApplication2
{
class Program
{
const String serviceAccountUserName = "mobileuser1";
const String serviceAccountPassword = "password123$";
const int UF_LOCKOUT = 0x0010;
const int UF_PASSWORD_EXPIRED = 0x800000;
static void Main(string[] args)
{
string line;
Console.WriteLine("Welcome to account validator V1.0.\n"+"Please enter the ldap address to proceed.");
Console.Write("\nEnter address: ");
String ldapAddress = Console.ReadLine();
try
{
if (ldapAddress != null)
{
Console.WriteLine("\nQuerying for users in " + ldapAddress);
//start of do-while
do
{
Console.WriteLine("\nPlease enter the user's account name to proceed.");
Console.Write("\nUsername: ");
String targetUserName = Console.ReadLine();
bool isValid = false;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress))
{
// validate the credentials
isValid = pc.ValidateCredentials(serviceAccountUserName, serviceAccountPassword);
// search AD data
DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapAddress, serviceAccountUserName, serviceAccountPassword);
//create instance fo the directory searcher
DirectorySearcher desearch = new DirectorySearcher(entry);
//set the search filter
desearch.Filter = "(&(sAMAccountName=" + targetUserName + ")(objectcategory=user))";
//find the first instance
SearchResult results = desearch.FindOne();
if (results != null)
{
//Check is account activated
bool isAccountActived = IsActive(results.GetDirectoryEntry());
if (isAccountActived) Console.WriteLine(targetUserName + "'s account is active.");
else Console.WriteLine(targetUserName + "'s account is inactive.");
//Check is account expired or locked
bool isAccountLocked = IsAccountLockOrExpired(results.GetDirectoryEntry());
if (isAccountLocked) Console.WriteLine(targetUserName + "'s account is locked or has expired.");
else Console.WriteLine(targetUserName + "'s account is not locked or expired.");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n\n");
}
else if (results == " ")
{
//no user entered
Console.WriteLine("No input detected!");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n");
}
else
{
//user does not exist
Console.WriteLine("User not found!");
Console.WriteLine("\nEnter bye to exit.");
Console.WriteLine("Press any key to continue.\n");
}
}//end of using
}//end of do
//leave console when 'bye' is entered
while ((line = Console.ReadLine()) != "bye");
}//end of if for ldap statement
else if (ldapAddress == " ")
{
Console.WriteLine("No input detected.");
Console.ReadLine();
Console.WriteLine("\nEnter bye to exit.");
Console.ReadLine();
Console.WriteLine("Press any key to continue.\n");
Console.ReadLine();
}
else
{
Console.WriteLine("Address not found!");
Console.ReadLine();
Console.WriteLine("\nEnter bye to exit.");
Console.ReadLine();
Console.WriteLine("Press any key to continue.\n");
Console.ReadLine();
}
}//end of try
catch (Exception e)
{
Console.WriteLine("Exception caught:\n\n" + e.ToString());
}
} //end of main void
static private bool IsActive(DirectoryEntry de)
{
if (de.NativeGuid == null) return false;
int flags = (int)de.Properties["userAccountControl"].Value;
return !Convert.ToBoolean(flags & 0x0002);
}
static private bool IsAccountLockOrExpired(DirectoryEntry de)
{
string attribName = "msDS-User-Account-Control-Computed";
de.RefreshCache(new string[] { attribName });
int userFlags = (int)de.Properties[attribName].Value;
return userFlags == UF_LOCKOUT || userFlags == UF_PASSWORD_EXPIRED;
}
}
}
您应该将
读线
放入一个循环中
string UserName = "";
do {
Console.Write("Username: ");
UserName = Console.ReadLine();
if (!string.IsNullOrEmpty(UserName)) {
Console.WriteLine("OK");
} else {
Console.WriteLine("Empty input, please try again");
}
} while (string.IsNullOrEmpty(UserName));
基本上,反复重复提示,直到用户输入的字符串不再为null或空。
最好的方法可能是创建一个新函数来获取非空输入:
private static string GetInput(string Prompt)
{
string Result = "";
do {
Console.Write(Prompt + ": ");
Result = Console.ReadLine();
if (string.IsNullOrEmpty(Result)) {
Console.WriteLine("Empty input, please try again");
}
} while (string.IsNullOrEmpty(Result));
return Result;
}
然后,您可以使用该函数获取输入,如:
static void Main(string[] args)
{
GetInput("Username");
GetInput("Password");
}
结果:
尝试使用以下代码:
(!string.IsNullOrEmpty(input));
(!string.IsNullOrEmpty(输入));您好,实用工具,谢谢您回答我的问题。这解决了问题吗?如果mark as answerHi Jens有帮助,我如何在“static void Main(string[]args)”方法中使用GetInput?当我尝试调用它时,我得到一个“非静态字段、方法或属性需要对象引用”错误。提前感谢!:)如果您阻止用户只输入空格,效果会更好。否则这是最好的方法@代码123对不起。CodeInNet是正确的,您还需要将函数标记为静态。这是从VB.NET中翻译出来的,在那里丢失了。@CodeInNet但这只是一个假设,即
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。输入的验证完全是另一个问题,可能依赖于上下文。对于整数呢?
(!string.IsNullOrEmpty(input));