C# 附加到csv文件(csv帮助器)会在csv文件中提供重复条目
使用下面的代码,我尝试将在控制台中输入的用户附加到现有的csv文件中。我正在使用CSV帮助程序,我阅读了文档 首先,我从csv文件加载用户,并使用以下代码将其存储在列表中:C# 附加到csv文件(csv帮助器)会在csv文件中提供重复条目,c#,list,linq,csv,csvhelper,C#,List,Linq,Csv,Csvhelper,使用下面的代码,我尝试将在控制台中输入的用户附加到现有的csv文件中。我正在使用CSV帮助程序,我阅读了文档 首先,我从csv文件加载用户,并使用以下代码将其存储在列表中: using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Globalization; using System.Linq; using CsvHelper; namespace Gimp
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Globalization;
using System.Linq;
using CsvHelper;
namespace GimpiesConsoleOOcsvListUI
{
public class UserList
{
public static List<User> LoadUsersFromCSV()
{
// using (var mem = new MemoryStream())
// using (var writer = new StreamWriter(mem))
using (var reader = new StreamReader("users.csv"))
using (var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture))
{
// csvReader.Configuration.Delimiter = ";";
// csvReader.Configuration.IgnoreBlankLines = true;
csvReader.Configuration.HasHeaderRecord = true;
// csvReader.Configuration.PrepareHeaderForMatch = (string header, int index) => header.ToLower();
// csvReader.Configuration.MissingFieldFound = null;
csvReader.Read();
csvReader.ReadHeader();
// Store all content inside a new List as objetcs
var users = csvReader.GetRecords<User>().ToList();
return users;
// try
// {
// }
// catch (CsvHelper.HeaderValidationException exception)
// {
// Console.WriteLine(exception);
// }
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Globalization;
using System.Linq;
using CsvHelper;
namespace GimpiesConsoleOOcsvListUI
{
public class RegisterManager
{
public void Register(List<User> users)
{
// Get user input
Console.WriteLine("Enter username:");
string username = Console.ReadLine();
Console.WriteLine("Enter email:");
string email = Console.ReadLine();
Console.WriteLine("Enter password:");
string password = Console.ReadLine();
Console.WriteLine("Enter userrole (typ admin, purchase or sales):");
string userrole = Console.ReadLine();
// Create fresh instance to save input in memory
User user = new User(username, email, password, userrole);
// Adds the user to the excisting list
users.Add(user);
FileOperations fo = new FileOperations();
// Calling the method from FileOperations.cs to write the List here to a CSV file
fo.WriteUsersToCSV(users);
}
}
}
在这里,您可以看到我添加了用户Bas,但Inkoop、Verkoop和Beheer再次添加
我在代码中找不到需要删除或更改的内容
这是我的程序。cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Globalization;
using System.Linq;
using CsvHelper;
namespace GimpiesConsoleOOcsvListUI
{
class Program
{
static void Main(string[] args)
{
// List of default users
// List<User> users = UserList.DefaultUsers();
// Working on it... Try to read csv file first and try to read list from method in UserList.cs
// UserList ul = new UserList();
List<User> users = UserList.LoadUsersFromCSV();
// List<User> users =
// ul.LoadUsersFromCSV(users);
// Create login instance
LoginManager loginMgr = new LoginManager();
// Create stock instance
Stock stock = new Stock();
Start:
// Welcome message
Console.WriteLine("Welcome to the Gimpies Console Application! Choose 1 to login or 2 to exit this application:");
// Get input from user
string input = Console.ReadLine();
// Set to false to check if login fails or not
bool successfull = false;
while (!successfull)
{
if(input == "1")
{
Console.WriteLine("Enter your username:");
string username = Console.ReadLine();
Console.WriteLine("Enter your password:");
string password = Console.ReadLine();
foreach (User user in users)
{
if (username == user.username && password == user.password && user.userrole == "admin")
{
// Create Admin instance to be able to call methods in that class
Admin am = new Admin();
// Calling the method in Admin.cs to start Menu logic
am.AdminLoggedIn(users);
successfull = true;
break;
}
if (username == user.username && password == user.password && user.userrole == "purchase")
{
// Create Purchase instance to be able to call methods in that class
Purchase pc = new Purchase();
// Calling the method in Purchase.cs to start Menu logic
pc.PurchaseLoggedIn(users);
successfull = true;
break;
}
if (username == user.username && password == user.password && user.userrole == "sales")
{
// Create Sales instance to be able to call methods in that class
Sales sl = new Sales();
// Calling the method in Sales.cs to start Menu logic
sl.SalesLoggedIn(users);
successfull = true;
break;
}
}
if (!successfull)
{
Console.WriteLine("Your username or password is incorrect, try again !!!");
}
}
else if (input == "2")
{
Environment.Exit(-1);
}
else if (input == "3")
{
FileOperations fo = new FileOperations();
// Calling the method from FileOperations.cs to write the List here to a CSV file
fo.WriteUsersToCSV(users);
goto Start;
}
else if (input == "4")
{
FileOperations fo = new FileOperations();
// Calling the method from FileOperations.cs to write the List here to a CSV file
fo.ReadUsersFromCSV();
goto Start;
}
else
{
Console.WriteLine("Try again !!!");
break;
}
}
// // Loop over stored users within instances inside the list where users are added
// foreach (User user in users)
// {
// if(loginMgr.Login(user))
// {
// // Login successfull
// Console.WriteLine("Login user " + user.UserName);
// }
// else
// {
// // Not successfull
// }
// }
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
利用制度全球化;
使用System.Linq;
使用CsvHelper;
命名空间GimpiesConsolleoCsvListui
{
班级计划
{
静态void Main(字符串[]参数)
{
//默认用户列表
//List users=UserList.DefaultUsers();
//正在处理它…首先尝试读取csv文件,然后尝试从UserList.cs中的方法读取列表
//UserList ul=新的UserList();
List users=UserList.LoadUsersFromCSV();
//列表用户=
//ul.LoadUsersFromCSV(用户);
//创建登录实例
LoginManager loginMgr=新的LoginManager();
//创建股票实例
股票=新股票();
开始:
//欢迎辞
WriteLine(“欢迎使用Gimpies控制台应用程序!选择1登录或选择2退出此应用程序:”;
//从用户处获取输入
字符串输入=Console.ReadLine();
//设置为false以检查登录是否失败
bool successfull=false;
而(!成功)
{
如果(输入=“1”)
{
Console.WriteLine(“输入您的用户名:”);
字符串username=Console.ReadLine();
Console.WriteLine(“输入密码:”);
字符串密码=Console.ReadLine();
foreach(用户中的用户)
{
if(username==user.username&&password==user.password&&user.userrole==admin)
{
//创建管理实例,以便能够调用该类中的方法
Admin am=新管理员();
//调用Admin.cs中的方法以启动菜单逻辑
am.AdminLoggedIn(用户);
成功=正确;
打破
}
if(username==user.username&&password==user.password&&user.userrole==“购买”)
{
//创建Purchase实例以便能够调用该类中的方法
购买pc=新购买();
//调用Purchase.cs中的方法以启动菜单逻辑
pc.PurchaseLoggedIn(用户);
成功=正确;
打破
}
if(username==user.username&&password==user.password&&user.userrole==sales)
{
//创建销售实例以便能够调用该类中的方法
销售sl=新销售();
//调用Sales.cs中的方法以启动菜单逻辑
sl.SalesLoggedIn(用户);
成功=正确;
打破
}
}
如果(!成功)
{
Console.WriteLine(“您的用户名或密码不正确,请重试!!!”;
}
}
否则如果(输入=“2”)
{
环境。退出(-1);
}
否则如果(输入=“3”)
{
FileOperations fo=新的FileOperations();
//从FileOperations.cs调用该方法以将此处的列表写入CSV文件
fo.WriteUsersToCSV(用户);
转到开始;
}
否则如果(输入=“4”)
{
FileOperations fo=新的FileOperations();
//从FileOperations.cs调用该方法以将此处的列表写入CSV文件
fo.ReadUsersFromCSV();
转到开始;
}
其他的
{
Console.WriteLine(“重试!!!”;
打破
}
}
////在添加用户的列表内的实例中循环存储的用户
//foreach(用户中的用户)
// {
//if(loginMgr.Login(用户))
// {
////登录成功
//Console.WriteLine(“登录用户”+user.UserName);
// }
//否则
// {
////没有成功
// }
// }
}
}
}
还有我的GitLab回购:我为您提供了两种选择。确保您只实现两个选项中的一个 选项#1(代码试图实现的选项),您可以将新记录附加到文件数据。此选项将要求您跟踪文件中已有的内容,以便您可以排除写入文件中已有的记录
username,email,password,userrole,
Inkoop,inkoop@gimpies.nl,123,purchase
Verkoop,verkoop@gimpies.nl,123,sales
Beheer,beheer@gimpies.nl,123,admin
Inkoop,inkoop@gimpies.nl,123,purchase
Verkoop,verkoop@gimpies.nl,123,sales
Beheer,beheer@gimpies.nl,123,admin
Bas,bas@bas.nl,123,admin
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Globalization;
using System.Linq;
using CsvHelper;
namespace GimpiesConsoleOOcsvListUI
{
class Program
{
static void Main(string[] args)
{
// List of default users
// List<User> users = UserList.DefaultUsers();
// Working on it... Try to read csv file first and try to read list from method in UserList.cs
// UserList ul = new UserList();
List<User> users = UserList.LoadUsersFromCSV();
// List<User> users =
// ul.LoadUsersFromCSV(users);
// Create login instance
LoginManager loginMgr = new LoginManager();
// Create stock instance
Stock stock = new Stock();
Start:
// Welcome message
Console.WriteLine("Welcome to the Gimpies Console Application! Choose 1 to login or 2 to exit this application:");
// Get input from user
string input = Console.ReadLine();
// Set to false to check if login fails or not
bool successfull = false;
while (!successfull)
{
if(input == "1")
{
Console.WriteLine("Enter your username:");
string username = Console.ReadLine();
Console.WriteLine("Enter your password:");
string password = Console.ReadLine();
foreach (User user in users)
{
if (username == user.username && password == user.password && user.userrole == "admin")
{
// Create Admin instance to be able to call methods in that class
Admin am = new Admin();
// Calling the method in Admin.cs to start Menu logic
am.AdminLoggedIn(users);
successfull = true;
break;
}
if (username == user.username && password == user.password && user.userrole == "purchase")
{
// Create Purchase instance to be able to call methods in that class
Purchase pc = new Purchase();
// Calling the method in Purchase.cs to start Menu logic
pc.PurchaseLoggedIn(users);
successfull = true;
break;
}
if (username == user.username && password == user.password && user.userrole == "sales")
{
// Create Sales instance to be able to call methods in that class
Sales sl = new Sales();
// Calling the method in Sales.cs to start Menu logic
sl.SalesLoggedIn(users);
successfull = true;
break;
}
}
if (!successfull)
{
Console.WriteLine("Your username or password is incorrect, try again !!!");
}
}
else if (input == "2")
{
Environment.Exit(-1);
}
else if (input == "3")
{
FileOperations fo = new FileOperations();
// Calling the method from FileOperations.cs to write the List here to a CSV file
fo.WriteUsersToCSV(users);
goto Start;
}
else if (input == "4")
{
FileOperations fo = new FileOperations();
// Calling the method from FileOperations.cs to write the List here to a CSV file
fo.ReadUsersFromCSV();
goto Start;
}
else
{
Console.WriteLine("Try again !!!");
break;
}
}
// // Loop over stored users within instances inside the list where users are added
// foreach (User user in users)
// {
// if(loginMgr.Login(user))
// {
// // Login successfull
// Console.WriteLine("Login user " + user.UserName);
// }
// else
// {
// // Not successfull
// }
// }
}
}
}
else if (input == "3")
{
FileOperations fo = new FileOperations();
// get existing users from file
List<User> userInFile = UserList.LoadUsersFromCSV();
// remove existing users from update list to create final list for writing
List<User> finalList = users.Where(u => !userInFile.Any(uf => uf.email.Equals(u.email, StringComparison.InvariantCultureIgnoreCase))).ToList();
fo.WriteUsersToCSV(users);
goto Start;
}
public void WriteUsersToCSV(List<User> users)
{
// overwrite the file each time; indicated by the `false` parameter
using (var writer = new StreamWriter("users.csv", false))
using (var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
// csvWriter.Configuration.HasHeaderRecord = false; // commented out as we write the whole file every time including the header
csvWriter.WriteRecords(users);
Console.WriteLine("New user added to users.csv");
}
}