【发布时间】:2020-08-17 22:35:13
【问题描述】:
通过以下代码,我尝试将在控制台中输入的用户附加到现有的 csv 文件中。我正在使用 CSV 助手并阅读了文档。
首先,我从 csv 文件加载用户,并使用以下代码将它们存储在一个列表中:
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);
}
}
}
然后输入的用户会使用WriteUsersToCSV的方法写入现有的csv文件:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Globalization;
using System.Linq;
using CsvHelper;
namespace GimpiesConsoleOOcsvListUI
{
// Handles CRUD within CSV files and able to save them
public class FileOperations
{
// Appends to CSV file from List
public void WriteUsersToCSV(List<User> users)
{
// Append to the file
using (var stream = File.Open("users.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
using (var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csvWriter.Configuration.HasHeaderRecord = false;
csvWriter.WriteRecords(users);
Console.WriteLine("New user added to users.csv");
}
}
// Writes to CSV file from List
public void WriteUsersToNewCSVFile(List<User> users)
{
using (var writer = new StreamWriter("users.csv"))
using (var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
// csvWriter.Configuration.Delimiter = ";";
csvWriter.Configuration.HasHeaderRecord = true;
csvWriter.Configuration.AutoMap<User>();
csvWriter.WriteHeader<User>();
csvWriter.WriteRecords(users);
Console.WriteLine("New file users.csv created!");
}
}
// Reads from CSV file and displays content from it
public void ReadUsersFromCSV()
{
using (var reader = new StreamReader("users.csv"))
using (var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture))
{
try
{
// 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 records = csvReader.GetRecords<User>().ToList();
// Loop through the List and show them in Console (connect to values in the constructor User.cs record.username etc..)
foreach (var record in records)
{
Console.WriteLine($"{record.username } | {record.email} | {record.password} | {record.userrole}");
}
}
catch (CsvHelper.HeaderValidationException exception)
{
Console.WriteLine(exception);
}
}
}
// Writes to CSV file from List
public void SaveGimpiesToCSV(List<Gimpies> gimpies)
{
// using (var mem = new MemoryStream())
// using (var writer = new StreamWriter(mem))
using (var writer = new StreamWriter("gimpies.csv"))
using (var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csvWriter.Configuration.Delimiter = ";";
csvWriter.Configuration.HasHeaderRecord = true;
csvWriter.Configuration.AutoMap<Gimpies>();
csvWriter.WriteHeader<Gimpies>();
csvWriter.WriteRecords(gimpies);
writer.Flush();
// var result = Encoding.UTF8.GetString(mem.ToArray());
// Console.WriteLine(result);
}
}
}
}
但是当我写入现有的 csv 文件时,我再次获得了前三个用户!查看结果:
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
在这里你看到我添加了用户 Bas,但是 Inkoop 和 Verkoop 和 Beheer 又被添加了。
我在代码中找不到需要删除或更改的内容。
确定这里是我的 Program.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
// }
// }
}
}
}
还有我的 GitLab 存储库:https://gitlab.com/marianydesign/gimpiesconsoleoocsvlist
【问题讨论】:
-
我看到
goto在那里.. 我的眼睛开始流泪了。如果您的问题源于代码的那部分,我不会感到惊讶。 -
使用免费的内置 Step Debugger 比您想象的要容易。 Here's how
-
无需深入研究代码,您似乎正在从 CSV 读取现有用户,添加新用户并附加到现有文件。(FileMode.Append)
-
感谢大家的反馈。我仍然每天都在学习。我知道
goto:-) 这也是更频繁地使用调试器的好建议。
标签: c# list linq csv csvhelper