[英]How to write a program with commands?
我想編寫一個程序來處理像這樣的命令
添加:並詢問您的名字、姓氏、年齡列表:列出目錄中的人員刪除:刪除詳細信息:顯示人員的詳細信息打開:打開文件。 (from json and xml) save: 保存文件(in json and xml)
到目前為止我在這個階段
我的問題如下....下面的代碼是文件,它沒有刪除或詳細信息之類的選項,添加
需要什么方法,鏈接等提示,不勝感激
string path2 = @"logowanie6.txt";
Console.WriteLine("Hej! Przedstaw się. Twoje dane będą zapisane w pliku o nazwie logowanie6.txt");
if (!File.Exists(path2))
{
using (StreamWriter sw = File.CreateText(path2))
{
sw.WriteLine("Osoby, które logowały się: ");
}
}
using (StreamWriter sw = File.AppendText(path2))
{
string namesurname = Console.ReadLine();
sw.WriteLine(namesurname);
}
using (StreamReader sr = File.OpenText(path2))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
幾個小時后,我做了這樣的東西……有人能告訴我下一個提示嗎?
class Person { public int Id { get; set; } public string Name { get; set; } } class FileExample { static void Main(string[] args) { while (true) { string choice = Console.ReadLine(); switch (choice) { case "Add": break; case "Update": break; case "Remove": break; case "Exit": return; } } } static List<Person> LoadList() { string text = File.ReadAllText("persons.txt"); if (string.IsNullOrEmpty(text)) return new List<Person>() { }; return JsonConvert.DeserializeObject<Person[]>(text).ToList(); } static void SaveList(IEnumerable<Person> persons) { File.WriteAllText("persons.txt", JsonConvert.SerializeObject(persons.ToArray())); } static void AddPerson(List<Person> list, Person newPerson) { list.Add(newPerson); } static void RemovePerson(List<Person> list, int personId) { var foundPerson = list.FirstOrDefault(x => x.Id == personId); if (foundPerson.= null) list;Remove(foundPerson); } }
也許試試這個: https://github.com/commandlineparser/commandline
只需創建多個動詞,如:list、add、remove、change、makeFun 等,而不是在一個進程中處理它們,只需從命令行調用 EXE 進行不同的操作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.