为什么我做出来只能保存一次,不能永久保存,只能用数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class Program
{
public int[] a = new int[100];
public string[] b = new string[100];
int r = 0;
public void tianjia()
{
Console.WriteLine("请输入你添加的用户名");
b[r] =Console.ReadLine();
Console.WriteLine("请输入你的密码");
a[r] = Convert.ToInt32(Console.ReadLine());
r++;
}
public void chazhao()
{
Console.WriteLine("请输入你要查找的用户名");
string j = Console.ReadLine();
Console.WriteLine("请输入密码:");
int mi = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < r; i++)
{
if (j == b[i] && mi == a[i])
{
Console.WriteLine("找到用户:用户名{0}密码{1}", b[i], a[i]);
}
else
{
Console.WriteLine("你输入的用户名或密码错误");
}
}
}
public void xiugai()
{
Console.WriteLine("请输入你要修改的用户名");
string xg = Console.ReadLine();
Console.WriteLine("请输入密码:");
int xm = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <r; i++)
{
if (xg == b[i] && xm == a[i])
{
Console.WriteLine("请输入新的用户名:");
string xin = Console.ReadLine();
Console.WriteLine("请输入新的密码");
int xi = Convert.ToInt32(Console.ReadLine());
b[i] = xin;
a[i] = xi;
}
}
}
public void xiansi()
{
for (int c = 0; c < r; c++)
{
Console.WriteLine("用户名:{0} 密码{1}", b[c], a[c]);
}
}
static void Main(string[] args)
{
Program sl = new Program();
Console.WriteLine("1是添加用户;2是查找用户;3是修改用户");
int mn = Convert.ToInt32(Console.ReadLine());
switch (mn)
{
case 1:
sl.tianjia();
break;
case 2:
sl.chazhao();
break;
case 3:
sl.xiugai();
break;
}
sl.xiansi();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class Program
{
public int[] a = new int[100];
public string[] b = new string[100];
int r = 0;
public void tianjia()
{
Console.WriteLine("请输入你添加的用户名");
b[r] =Console.ReadLine();
Console.WriteLine("请输入你的密码");
a[r] = Convert.ToInt32(Console.ReadLine());
r++;
}
public void chazhao()
{
Console.WriteLine("请输入你要查找的用户名");
string j = Console.ReadLine();
Console.WriteLine("请输入密码:");
int mi = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < r; i++)
{
if (j == b[i] && mi == a[i])
{
Console.WriteLine("找到用户:用户名{0}密码{1}", b[i], a[i]);
}
else
{
Console.WriteLine("你输入的用户名或密码错误");
}
}
}
public void xiugai()
{
Console.WriteLine("请输入你要修改的用户名");
string xg = Console.ReadLine();
Console.WriteLine("请输入密码:");
int xm = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <r; i++)
{
if (xg == b[i] && xm == a[i])
{
Console.WriteLine("请输入新的用户名:");
string xin = Console.ReadLine();
Console.WriteLine("请输入新的密码");
int xi = Convert.ToInt32(Console.ReadLine());
b[i] = xin;
a[i] = xi;
}
}
}
public void xiansi()
{
for (int c = 0; c < r; c++)
{
Console.WriteLine("用户名:{0} 密码{1}", b[c], a[c]);
}
}
static void Main(string[] args)
{
Program sl = new Program();
Console.WriteLine("1是添加用户;2是查找用户;3是修改用户");
int mn = Convert.ToInt32(Console.ReadLine());
switch (mn)
{
case 1:
sl.tianjia();
break;
case 2:
sl.chazhao();
break;
case 3:
sl.xiugai();
break;
}
sl.xiansi();
}
}
}
解决方案 »
- winform在标题栏上加控件,在XP系统上可以显示,在win7上不能显示
- 使用list的一个问题
- this.close()不能用在load事件里怎么办
- 着急 将一小段C代码翻译成C#代码(100分)
- int i = new int()与 int i的区别
- 在winfrm里,如何判断某一节点是viewtree的第一根节点啊,谢就一个字,说多少次都行
- 请问:写软件需求文档,有没什么好用的软件?,谢谢!
- 关于treeview 的 checkbox 的使用
- 在windows窗体程序中实现同一界面中两个button(可以移动)的连线问题(100)
- 请问如何在安装程序里加入建立指向web项目首页的快捷方式?
- 关于C#调用Delphi的dll
- DataGridView如何改变选中行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;namespace 将数组转成xml读取
{
class Program
{
static void Main(string[] args)
{
Person[] o = Init().ToArray();
///
/// 此处可理解成:
/// 要转化的对象是什么类型,以转化格式的时候按此类型格式进行转化
Type type = o.GetType(); //string a = type.FullName;
string path = Directory.GetCurrentDirectory()+@"\test.xml";
WriteXmlSerializer(type, o,path);
ReadeXmlSerializer(type,path);
}
/// <summary>
/// 读取XML,并将其转化成数组
/// </summary>
/// <param name="type"></param>
private static void ReadeXmlSerializer(Type type,string path)
{
string temp = "";
Person[] o = new Person[10];
//模拟读取数据库数据
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
temp = sr.ReadToEnd(); //将xml转成object[]
XmlSerializer xml = new XmlSerializer(type);
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(temp)))
{
try
{
o = (Person[])xml.Deserialize(ms);
}
catch (Exception)
{
throw;
}
}
}
/// <summary>
/// 将数组转化成XML,
/// </summary>
/// <param name="type"></param>
/// <param name="o"></param>
private static void WriteXmlSerializer(Type type, Person[] o, string path)
{
string temp = "";
DataContractSerializer dcs = new DataContractSerializer(type);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(ms))
{
dcs.WriteObject(xw,o);
}
ms.Position = 0;
temp = System.Xml.Linq.XElement.Load(ms).ToString();
}
}
/// <summary>
/// 初使化学生集合
/// </summary>
/// <returns></returns>
private static List<Person> Init()
{
List<Person> students = new List<Person>(); Student s0 = new Student { id = 1, name = "张三", sex = false, age = 12, address = "aaaaa" };
Student s1 = new Student { id = 2, name = "李四", sex = true, age = 22, address = "bbbbbb" };
Student s2 = new Student { id = 8, name = "王五", sex = false, age = 16, address = "cccc" };
Student s3 = new Student { id = 14, name = "赵六", sex = true, age = 14, address = "ddddd" };
Student s4 = new Student { id = 6, name = "二麻子", sex = true, age = 32, address = "eeeee" };
Student s5 = new Student { id = 3, name = "w", sex = false, age = 26, address = "gggggg" };
Student s6 = new Student { id = 10, name = "fgg", sex = false, age = 22, address = "jjjjj" };
Student s7 = new Student { id = 4, name = "qq", sex = true, age = 23, address = "wwwww" }; Teacher t1 = new Teacher() { Id = 1, Name = "aaa", Sex = false };
Teacher t2 = new Teacher() { Id = 2, Name = "bbb", Sex = true };
Teacher t3 = new Teacher() { Id = 3, Name = "ccc", Sex = true };
students.Add(s0);
students.Add(s1);
students.Add(s2);
students.Add(s3);
students.Add(s4);
students.Add(s5);
students.Add(s6);
students.Add(s7); students.Add(t1);
students.Add(t2);
students.Add(t3);
return students;
}
WriteXmlSerializer(type, o,path);
ReadeXmlSerializer(type,path);实体也不要弄那么多个, 多类个继承 时 还要处理其它 东西,所以你只需要 写一个实体测试类就好了