这个项目分为四个文件夹:DllInvoke.cs (实现全屏效果),Stu.cs(实现注册和登录),Information.cs(实现学员信息的录入,显示、查询、添加、修改和删除),Program.cs(菜单操作和main方法)
DllInvoke.cs:(友情提示,using System.Runtime.InteropServices; 需要引入该命名空间)
public class DllInvoke
{
#region Win API
[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(string path);
[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr lib, string funcName);
[DllImport("kernel32.dll")]
private extern static bool FreeLibrary(IntPtr lib);
#endregion
private IntPtr hLib;
public DllInvoke(String DLLPath)
{
hLib = LoadLibrary(DLLPath);
}
~DllInvoke()
{
FreeLibrary(hLib);
}
//将要执行的函数转换为委托
public Delegate Invoke(string APIName, Type t)
{
IntPtr api = GetProcAddress(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
}
Stu.cs: (友情提示:using System.IO; using System.Threading; 需要引入)
public class Stu
{
#region 判断用户信息记事本是否存在
private static bool IsNaNStuTtxt()
{
bool flag = false;
if (File.Exists(@"stu.txt"))
{
flag = true;
}
return flag;
}
#endregion
#region 将用户名和密码写入到记事本里面
private static void Writter(string user, string pwd)
{
bool flag = IsNaNStuTtxt();
FileStream fs;
if (flag)
{
//如果记事本数据存在,则追加数据
fs = new FileStream(@"stu.txt", FileMode.Append);
}
else
{
//如果记事本数据不存在,则创建记事本
fs = new FileStream(@"stu.txt", FileMode.Create);
}
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine(user + ":" + pwd);
sw.Close();
fs.Close();
}
#endregion
#region 输入用户名和密码,并将密码进行加密, 并保存到string数组里面
private static string[] LuRu(string str)
{
Console.WriteLine(str);
Console.Write("请输入用户名:");
string user = Console.ReadLine();
Console.Write("请输入密码:");
string pwd = Console.ReadLine();
char[] ch = pwd.ToCharArray();//将密码字符串转换为char数组
//将密码进行加密
for (int i = 0; i < ch.Length; i++)
{
ch[i] += (char)5;
}
Array.Reverse(ch);//将加密的密码进行顺序反转
/*****/
//将char数组转换为字符串的方式
StringBuilder sb = new StringBuilder();
sb.Append(ch);
string pwd1 = sb.ToString();
/*****/
string[] stuss = new string[2];
stuss[0] = user;
stuss[1] = pwd1;
return stuss;
}
#endregion
#region 用户注册 (可以实现多用户注册)
public static void Register()
{
string[] str = LuRu("欢迎进入注册界面!");
Writter(str[0], str[1]);
Console.WriteLine("注册成功!");
Login();
}
#endregion
#region 从记事本读取用户账户和密码
private static string[] Reader()
{
bool flag = IsNaNStuTtxt();
if (flag)
{
StreamReader sr = new StreamReader(@"stu.txt", Encoding.UTF8);
string str = sr.ReadToEnd();
str = str.Trim();//去掉后面的空格 这块要注意,不然是不能登录成功的!
if(str.Contains("\n"))//字符串里面是否包含“\n”
{
string[] s = str.Split(new char[]{‘\r‘,‘\n‘});//这里注意,因为后面是跟着\r\n的,所以这里这样的分割
return s;
}else
{
string[] s = str.Split(‘:‘);
return s;
}
}
return null;
}
#endregion
#region 用户登录
public static void Login()
{
string[] str = LuRu("欢迎进入登录界面!");
string user = str[0];
string pwd = str[1];
string[] Rstr = Reader();
bool flag = false;
if (Rstr != null)
{
if (Rstr[0].Contains(":"))//是否包含:
{
//当注册不止一条数据的时候
for (int i = 0; i < Rstr.Length; i++)
{
//当包含:的时候,说明是多个个注册的信息
//这个时候循环变量,通过:分割,取到该数据的第一个和用户名比较,在取到:后面第二个数据和密码进行比较
if(Rstr[i].Split(‘:‘)[0].Equals(user)&&Rstr[i].Split(‘:‘)[1].Equals(pwd))
{
flag = true;
}
}
if (flag)
{
Console.WriteLine("登录成功!");
}
else
{
Console.WriteLine("用户名或者密码错误!");
}
}else
{
//当最开始注册,只有一条数据,可以这么操作
if (user.Equals(Rstr[0]) && pwd.Equals(Rstr[1]))
{
Console.WriteLine("登录成功!");
}
else
{
Console.WriteLine("用户名或者密码错误!");
}
}
//当flag标记如果被修改为true的时候,说明存在该账户和密码
}
}
#endregion
#region 进度条
public static void Bar()
{
bool isbreak = false;
ConsoleColor colorBack = Console.BackgroundColor;//背景色
ConsoleColor colorFore = Console.ForegroundColor;//前景色
Console.WriteLine("*************************************");
Console.BackgroundColor = ConsoleColor.DarkCyan;
for (int i = 0; i < Console.WindowWidth - 3; i++)
{
Console.Write(" ");
}
Console.WriteLine(" ");
Console.BackgroundColor = colorBack;
Console.WriteLine("0%");
Console.WriteLine("<按【Enter】键停止>");
for (int i = 0; i <= 100; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter)
{
isbreak = true;
break;
}
Console.BackgroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(i * (Console.WindowWidth - 2) / 100, 1);
Console.Write(" ");
Console.BackgroundColor = colorBack;
Console.ForegroundColor = ConsoleColor.Green;
Console.SetCursorPosition(0, 2);
Console.Write("{0}%", i);
Console.ForegroundColor = colorFore;
Thread.Sleep(10);//可以改变进度条的速度
}
Console.SetCursorPosition(0, 3);
Console.Write(isbreak ? "停止!!!" : "完成");
// Console.WriteLine(" ");
//Console.ReadKey();
Console.Clear();//清屏
}
#endregion
}
Information.cs (友情提示:using System.IO; 需要引入)
还需要在class和namespace之间写个结构体
public struct Student
{
public string no;//学号
public string name;//姓名
public int age;//年龄
public string gender;//性别
public string tel;//电话
public string address;//住址
}
public class Information
{
static Student[]stus=new Student[100];
#region 判断记事本是否存在 公共类
private static bool IsNanText()
{
bool flag = false;
if (File.Exists(@"Information.txt"))
{
flag = true;
}
return flag;
}
#endregion
#region 写入数据到记事本
private static void Writter(string str, int num)
{
bool flag = IsNanText();
if (flag)
{
if (num > 0)
{
File.Delete(@"Information.txt");//删除文件
FileStream fs = new FileStream(@"Information.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine(str);
sw.Close();
fs.Close();
}
else
{
FileStream fs = new FileStream(@"Information.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine(str);
sw.Close();
fs.Close();
}
}
else
{
FileStream fs = new FileStream(@"Information.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine(str);
sw.Close();
fs.Close();
}
}
#endregion
#region 录入学员信息 ①
public static void Stu_LuRu()
{
bool flag = IsNanText();
if(flag)
{
Console.WriteLine("欢迎进入添加学员菜单:");
Stu_Insert();
}else
{
Console.WriteLine("请输入要录入的学员个数:");
int num = Convert.ToInt32(Console.ReadLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num; i++)
{
bool f = false;
do
{
Console.Write("请输入要录入第{0}个学员的学号:", i + 1);
string no = Console.ReadLine();
f = IsNo_Reader(no);
if(f)
{
stus[i].no = Console.ReadLine();
}else
{
Console.WriteLine("学号已经存在");
}
} while (f==false);
Console.Write("请输入要录入第{0}个学员的姓名:", i + 1);
stus[i].name = Console.ReadLine();
Console.Write("请输入要录入第{0}个学员的年龄:", i + 1);
stus[i].age = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入要录入第{0}个学员的性别:", i + 1);
stus[i].gender = Console.ReadLine();
Console.Write("请输入要录入第{0}个学员的电话:", i + 1);
stus[i].tel = Console.ReadLine();
Console.Write("请输入要录入第{0}个学员的住址:", i + 1);
stus[i].address = Console.ReadLine();
}
Console.WriteLine("你输入写学员信息如下:");
Console.WriteLine("学号\t姓名\t年龄\t性别\t电话\t住址");
sb.AppendLine("学号\t姓名\t年龄\t性别\t电话\t住址");
for (int i = 0; i < num; i++)
{
Console.WriteLine(stus[i].no + "\t" + stus[i].name +
"\t" + stus[i].age + "\t" + stus[i].gender +
"\t" + stus[i].tel + "\t" + stus[i].address);
sb.AppendLine(stus[i].no + "\t" + stus[i].name +
"\t" + stus[i].age + "\t" + stus[i].gender +
"\t" + stus[i].tel + "\t" + stus[i].address);
}
string s = sb.ToString().Trim();
Writter(s, 0);
Console.WriteLine("录入的学员信息如下:");
Stu_Reader();
}
}
#endregion
#region 学员信息读取 ②
public static void Stu_Reader()
{
bool flag = IsNanText();
if (flag)
{
StreamReader sr = new StreamReader(@"Information.txt", Encoding.UTF8);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
}
else
{
Console.WriteLine("学员信息还没有创建和录入,请选录入!");
Stu_LuRu();
}
}
#endregion
#region 获取记事本的信息 公共类
private static string Info_Reader()
{
StreamReader sr = new StreamReader(@"Information.txt", Encoding.UTF8);
string str = sr.ReadToEnd();
sr.Close();
return str;
}
#endregion
#region 读取记事本数据,获取录入学员总个数
private static int Info_num()
{
string str = Info_Reader();
//Replace替换 string类的方法
/* Replace("被替换值", "替换值");*/
str = str.Replace("\r\n", "\n");
char[] ch = str.ToCharArray();
int num = 0;//计数器
for (int i = 0; i < ch.Length; i++)
{
if (ch[i] == ‘\n‘)
{
num++;
}
}
num = num - 1;
return num;
}
#endregion
#region 添加学员信息 ③
public static void Stu_Insert()
{
bool flag = IsNanText();
if (flag)
{
int num = Info_num();//总人数
Console.WriteLine("请输入要添加的学员个数:");
int count = Convert.ToInt32(Console.ReadLine());
StringBuilder sb = new StringBuilder();
for (int i = num; i < num + count; i++)
{
stus[i].no =Stu_no(i);//自动得到下一位学号免得手动录入相同的
Console.Write("请输入要添加第{0}个学员的姓名:", i + 1);
stus[i].name = Console.ReadLine();
Console.Write("请输入要添加第{0}个学员的年龄:", i + 1);
stus[i].age = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入要添加第{0}个学员的性别:", i + 1);
stus[i].gender = Console.ReadLine();
Console.Write("请输入要添加第{0}个学员的电话:", i + 1);
stus[i].tel = Console.ReadLine();
Console.Write("请输入要添加第{0}个学员的住址:", i + 1);
stus[i].address = Console.ReadLine();
Console.WriteLine(stus[i].no + "\t" + stus[i].name +
"\t" + stus[i].age + "\t" + stus[i].gender +
"\t" + stus[i].tel + "\t" + stus[i].address);
sb.AppendLine(stus[i].no + "\t" + stus[i].name +
"\t" + stus[i].age + "\t" + stus[i].gender +
"\t" + stus[i].tel + "\t" + stus[i].address);
//Trim() 去空格
}
string s = sb.ToString().Trim();
Writter(s, 0);
Console.WriteLine("学员信息如下:");
Stu_Reader();
}
else
{
Console.WriteLine("学员信息不存在,请先录入学员信息:");
Stu_LuRu();
}
}
#endregion
#region 将记事本里面取到的数据装入到string数组
public static string[] Stu_ReaderInfo()
{
StreamReader sr = new StreamReader(@"Information.txt", Encoding.UTF8);
string str = sr.ReadToEnd().Trim();
//替换
str = str.Replace("\r\n", "\n");
string[] s = str.Split(‘\n‘);
sr.Close();
return s;
}
#endregion
#region 将从记事本取到的数据保存到结构体数组里面 公共方法
private static Student[] Stu_getStu()
{
//取到记事本数据
string[] str = Stu_ReaderInfo();
int num = Info_num();//取到学员信息总条数
Student[] stus_s = new Student[num];
for (int i = 0; i < str.Length; i++)
{
if (i == 0)
{
continue;
}
else
{
//因为i从1开始
//下标从0开始
/*
str[i].Split(‘\t‘)
{string[6]}
[0]: "001"
[1]: "jack"
[2]: "19"
[3]: "男"
[4]: "12345"
[5]: "湖北"
* str[1]="001\tjack\t19\t男\t12345\t湖北"
str[i].Split(‘\t‘)[0]
"001"
*/
stus_s[i - 1].no = str[i].Split(‘\t‘)[0];
stus_s[i - 1].name = str[i].Split(‘\t‘)[1];
stus_s[i - 1].age = Convert.ToInt32(str[i].Split(‘\t‘)[2]);
stus_s[i - 1].gender = str[i].Split(‘\t‘)[3];
stus_s[i - 1].tel = str[i].Split(‘\t‘)[4];
stus_s[i - 1].address = str[i].Split(‘\t‘)[5];
}
}
return stus_s;
}
#endregion
#region 学员信息查询 ④
public static void Stu_Search()
{
bool flag = IsNanText();//判断记事本数据是否存在
bool f = false;//标记,记录学员是否存在
if (flag)
{
//先显示一下
Stu_Reader();
Student[] s = Stu_getStu();//取到结构体数组
Console.Write("请输入要查询的学号:");
string no = Console.ReadLine();
for (int i = 0; i < s.Length; i++)
{
if (s[i].no.Equals(no))
{
f = true;
Console.WriteLine("查找的学员信息如下:");
Console.WriteLine(s[i].no + "\t" + s[i].name +
"\t" + s[i].age + "\t" + s[i].gender +
"\t" + s[i].tel + "\t" + s[i].address);
}
}
if (f == false)
{
Console.WriteLine("你查找的学员信息不存在!");
}
}
else
{
Console.WriteLine("你还未创建学员信息,请先录入学员信息:");
Stu_LuRu();
}
}
#endregion
#region 查询学号,找到后,修改电话操作 ⑤
public static void Stu_Update()
{
bool flag = IsNanText();
bool f = false;//标记
if (flag)
{
Student[] s = Stu_getStu();
Console.WriteLine("学员信息如下:");
Stu_Reader();//显示
do
{
Console.Write("请输入要查找的学号:");
string no = Console.ReadLine();
for (int i = 0; i < s.Length; i++)
{
if (s[i].no.Equals(no))
{
f = true;
Console.Write("请输入要修改的电话:");
string tel = Console.ReadLine();
s[i].tel = tel;
Console.WriteLine("修改后的信息如下:");
Console.WriteLine(s[i].no + "\t" + s[i].name +
"\t" + s[i].age + "\t" + s[i].gender +
"\t" + s[i].tel + "\t" + s[i].address);
}
}
if (f == false)
{
Console.WriteLine("你查找的学员不存在!");
}
} while (f == false);
//得到记事本数据,装入到结构体数组
Console.WriteLine("学员信息如下:");
StringBuilder sb = new StringBuilder();
Console.WriteLine("学号\t姓名\t年龄\t性别\t电话\t住址");
sb.AppendLine("学号\t姓名\t年龄\t性别\t电话\t住址");
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(s[i].no + "\t" + s[i].name +
"\t" + s[i].age + "\t" + s[i].gender +
"\t" + s[i].tel + "\t" + s[i].address);
sb.AppendLine(s[i].no + "\t" + s[i].name +
"\t" + s[i].age + "\t" + s[i].gender +
"\t" + s[i].tel + "\t" + s[i].address);
}
string str = sb.ToString().Trim();
//需要把原来的记事本数据删除,然后从新创建,数据加入到记事本
Writter(str, 1);
}
else
{
Console.WriteLine("学员信息不存在!请先录入学员信息:");
Stu_LuRu();
}
}
#endregion
#region 根据学号查询后,进行删除 ⑥
public static void Stu_Delete()
{
//判断记事本是否存在
bool flag = IsNanText();
bool f = false;//标记
if (flag)
{
//得到从记事本取出来的数据
Student[] s = Stu_getStu();
int count = s.Length;
string y_n = string.Empty;
do
{
do
{
Stu_Reader();//显示
Console.Write("请输入要删除的学员学号:");
string no = Console.ReadLine();
if (count == 1)
{
Console.WriteLine("只剩下一条数据,不允许进行删除!");
break;
}
for (int i = 0; i < count; i++)
{
if (s[i].no.Equals(no))
{
f = true;
count--;
for (int j = i; j < count; j++)
{
s[j].no = s[j + 1].no;
s[j].name = s[j + 1].name;
s[j].age = s[j + 1].age;
s[j].gender = s[j + 1].gender;
s[j].tel = s[j + 1].tel;
s[j].address = s[j + 1].address;
}
}
}
if (f == false)
{
Console.WriteLine("未查到该学员!");
}
} while (f == false);
Console.WriteLine("学员信息如下");
StringBuilder sb = new StringBuilder();
Console.WriteLine("学号\t姓名\t年龄\t性别\t电话\t住址");
sb.AppendLine("学号\t姓名\t年龄\t性别\t电话\t住址");
for (int k = 0; k < count; k++)
{
Console.WriteLine(s[k].no + "\t" + s[k].name +
"\t" + s[k].age + "\t" + s[k].gender +
"\t" + s[k].tel + "\t" + s[k].address);
sb.AppendLine(s[k].no + "\t" + s[k].name +
"\t" + s[k].age + "\t" + s[k].gender +
"\t" + s[k].tel + "\t" + s[k].address);
}
string s_1 = sb.ToString().Trim();
Writter(s_1, 1);
Console.Write("是否要继续删除?(y/n)");
y_n = Console.ReadLine();
// y_n.ToLower()转换成小写
} while (y_n.ToLower().Equals("y"));
}
}
#endregion
#region 查询学号是否存在 ⑦
private static bool IsNo_Reader(string no)
{
bool f = false;
Student[] s = Stu_getStu();//取到结构体数组
for (int i = 0; i < s.Length; i++)
{
if (s[i].no.Equals(no))
{
f = true;
}
}
return f;
}
#endregion
#region 自动得到下一位学号 ⑧
private static string Stu_no(int i)
{
string s = i.ToString();
string no = string.Empty;//声明一个为""的no
if (s.Length > 1)
{
no = "0" + i;
}
else
{
no = "00" + i;
}
return no;
}
#endregion
}
Program.cs
友情提示:
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
需要引入
class Program
{
#region Win API
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int nStdHandle);
const int STD_OUTPUT_HANDLE = -11;
#endregion
public delegate bool SetConsoleDisplayMode(IntPtr hOut, int dwNewMode, out int lpdwOldMode);
static void Main(string[] args)
{
#region 实现全屏
DllInvoke dll = new DllInvoke("kernel32.dll");
int dwOldMode;
//标准输出句柄
IntPtr hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//调用Win API,设置屏幕最大化
SetConsoleDisplayMode s = (SetConsoleDisplayMode)dll.Invoke("SetConsoleDisplayMode", typeof(SetConsoleDisplayMode));
s(hOut, 1, out dwOldMode);
Console.WriteLine("********************鲍鲍学员管理系统欢迎你********************");
#endregion
Menu_Play();
}
#region 进入菜单
static void Menu()
{
Thread.Sleep(50);
Console.WriteLine("*********************");
Thread.Sleep(50);
Console.WriteLine("*********************");
Thread.Sleep(50);
Console.WriteLine("欢迎来到学员管理系统");
Thread.Sleep(50);
Console.WriteLine("*********************");
Thread.Sleep(50);
Console.WriteLine("*********************");
Thread.Sleep(50);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Thread.Sleep(50);
Console.WriteLine("********************************");
Thread.Sleep(50);
Console.WriteLine("*********************************");
Thread.Sleep(50);
Console.WriteLine("***1、注册用户账户***************");
Thread.Sleep(50);
Console.WriteLine("***2、登录用户账户****************");
Thread.Sleep(50);
Console.WriteLine("********************************");
Thread.Sleep(50);
Console.WriteLine("*********************************");
}
#endregion
#region 进入菜单操作
static void Menu_Play()
{
while (true)
{
Menu();
Console.WriteLine("请选择菜单项:");
string str = Console.ReadLine();
switch (str)
{
case "1":
Stu.Register();
break;
case "2":
Stu.Login();
Console.Clear();//清屏
Stu.Bar();//进度条
Console.Clear();//清屏
Menu_Info_Play();//调用菜单操作
break;
default:
Console.WriteLine("输入有误!请重新输入菜单项:");
break;
}
}
}
#endregion
#region 菜单项
static void Menu_Info()
{
Thread.Sleep(100);//休眠100毫秒在进行
Console.WriteLine("*************************************");
Thread.Sleep(100);
Console.WriteLine("*************************************");
Thread.Sleep(100);
Console.WriteLine("***1、录入学员信息*******************");
Thread.Sleep(100);
Console.WriteLine("***2、显示学员信息*******************");
Thread.Sleep(100);
Console.WriteLine("***3、添加学员信息*******************");
Thread.Sleep(100);
Console.WriteLine("***4、查询学员信息*******************");
Thread.Sleep(100);
Console.WriteLine("***5、修改学员信息*******************");
Thread.Sleep(100);
Console.WriteLine("***6、删除学员信息*******************");
Thread.Sleep(100);
Console.WriteLine("***7、退出系统***********************");
Thread.Sleep(100);
Console.WriteLine("*************************************");
Thread.Sleep(100);
Console.WriteLine("*************************************");
}
#endregion
#region 菜单项操作
static void Menu_Info_Play()
{
while (true)
{
Menu_Info();
Console.WriteLine("请选择菜单项:");
string str = Console.ReadLine();
switch (str)
{
case "1":
Information.Stu_LuRu();
break;
case "2":
Information.Stu_Reader();
break;
case "3":
Information.Stu_Insert();
break;
case "4":
Information.Stu_Search();
break;
case "5":
Information.Stu_Update();
break;
case "6":
Information.Stu_Delete();
break;
case "7":
Console.WriteLine("是否决定要退出系统?(y/n)");
string str1 = Console.ReadLine();
if (str1.ToLower().Equals("y"))
{
Process.GetCurrentProcess().Kill();//退出系统
}
break;
default:
Console.WriteLine("你选择的菜单项不存在!请重新输入!");
break;
}
}
}
#endregion
}