Can';t将列表附加到现有文件。C#
**我有一个现有的.txt文件,我想用它来存储我的数据,但是当使用这段代码时,我在开关案例1的第39行得到一个错误。我不熟悉stackoverflow和一般的编码,所以请原谅我犯了一些错误:)**Can';t将列表附加到现有文件。C#,c#,list,file,append,write,C#,List,File,Append,Write,**我有一个现有的.txt文件,我想用它来存储我的数据,但是当使用这段代码时,我在开关案例1的第39行得到一个错误。我不熟悉stackoverflow和一般的编码,所以请原谅我犯了一些错误:)** 使用系统; 使用System.Collections.Generic; 使用System.IO; 班级计划 { 公共静态列表站点=新列表(); 静态void Main(字符串[]参数) { 字符串文件=@“C:\Users\james\Documents\DataFolder\Vault.txt”;
使用系统;
使用System.Collections.Generic;
使用System.IO;
班级计划
{
公共静态列表站点=新列表();
静态void Main(字符串[]参数)
{
字符串文件=@“C:\Users\james\Documents\DataFolder\Vault.txt”;
string命令=”;
while(命令!=“退出”)
{
Console.Clear();
Console.WriteLine(“请输入命令:”);
command=Console.ReadLine().ToLower();
开关(命令)
{
案例“1”:
AddPw();
附录行(文件,Pw.Site);
打破
案例“2”:
如果(File.Exists(File))
{
//在一个字符串中读取所有内容
//并显示字符串
string str=File.ReadAllText(文件);
控制台写入线(str);
}
打破
}
}
}
私有静态void AddPw()
{
Pw Pw=新Pw();
Console.Write(“输入用户名/电子邮件:”);
pw.Username=Console.ReadLine();
控制台。写入(“输入全名:”);
pw.FullName=Console.ReadLine();
控制台。写入(“输入电话号码:”);
pw.PhoneNumber=Console.ReadLine();
控制台。写入(“输入密码:”);
字符串密码=Console.ReadLine();
密码=密码;
地点。添加(pw);
}
专用静态无效打印Pw(Pw Pw)
{
Console.WriteLine(“用户名/电子邮件:+pw.Username”);
Console.WriteLine(“全名:+pw.FullName”);
Console.WriteLine(“电话号码:+pw.PhoneNumber”);
Console.WriteLine(“密码:+pw.Password[0]);
Console.WriteLine(“-----------------------------------------”;
}
私有静态void ListPw()
{
如果(Site.Count==0)
{
Console.WriteLine(“您的通讯簿是空的,请按任意键继续。”);
Console.ReadKey();
返回;
}
Console.WriteLine(“以下是您通讯簿中的当前联系人:\n”);
foreach(现场的var pw)
{
PrintPw(pw);
}
Console.WriteLine(“\n按任意键继续”);
Console.ReadKey();
}
}
公共类
{
公共字符串用户名{get;set;}
公共字符串全名{get;set;}
公共字符串PhoneNumber{get;set;}
公共字符串密码{get;set;}
}
在这一行中,需要传递IEnumerable以使AppendAllLines正常工作。您可以使用ConvertAll方法轻松地将站点(即List
)转换为IEnumerable
。以下是实现这一目标的一种方法:
将该行替换为以下内容:
File.AppendAllLines(file, Site.ConvertAll<string>(
(p) => string.Format("{0} | {1} | {2} | {3}\n",
p.Username,
p.FullName,
p.PhoneNumber,
p.Password
))
);
File.AppendAllLines(文件,Site.ConvertAll(
(p) =>string.Format(“{0}{1}{2}{3}\n”,
p、 用户名,
p、 全名,
p、 电话号码,
p、 密码
))
);
这个“lambda”基本上接受您的Pw对象并将其转换为一个内联字符串。我已经更新了您现有的函数
使用此功能,您可以在现有文件中添加和追加数据。
欢迎光临,请您将错误信息包括进来,并告诉我们您已经尝试了什么?似乎
Pw
不包含Site
属性。
File.AppendAllLines(file, Pw.Site);
File.AppendAllLines(file, Site.ConvertAll<string>(
(p) => string.Format("{0} | {1} | {2} | {3}\n",
p.Username,
p.FullName,
p.PhoneNumber,
p.Password
))
);
private static void AddPw(string filePath)
{
try
{
Pw pw = new Pw();
if (!File.Exists(filePath))
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath))
{
Console.Write("Enter the Username/Email: ");
pw.Username = Console.ReadLine();
sw.WriteLine(pw.Username);
Console.Write("Enter Full Name: ");
pw.FullName = Console.ReadLine();
sw.WriteLine(pw.FullName);
Console.Write("Enter Phone Number: ");
pw.PhoneNumber = Console.ReadLine();
sw.WriteLine(pw.PhoneNumber);
Console.Write("Enter Your Password: ");
pw.Password = Console.ReadLine();
sw.WriteLine(pw.Password);
}
}
else
{
using (StreamWriter sw = File.AppendText(filePath))
{
Console.Write("Enter the Username/Email: ");
pw.Username = Console.ReadLine();
sw.WriteLine(pw.Username);
Console.Write("Enter Full Name: ");
pw.FullName = Console.ReadLine();
sw.WriteLine(pw.FullName);
Console.Write("Enter Phone Number: ");
pw.PhoneNumber = Console.ReadLine();
sw.WriteLine(pw.PhoneNumber);
Console.Write("Enter Your Password: ");
pw.Password = Console.ReadLine();
sw.WriteLine(pw.Password);
}
}
}
catch (Exception ex)
{
}
}