C# 在控制台应用程序中编辑文本

C# 在控制台应用程序中编辑文本,c#,.net,xml,command-line,console-application,C#,.net,Xml,Command Line,Console Application,我写了这段代码 using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace ContactManagement { class Program { static void Main(string[] args) { FileManager<List<Contact>> dbObj = new FileManager&l

我写了这段代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ContactManagement
{
class Program
{
static void Main(string[] args)
{
FileManager<List<Contact>> dbObj = new FileManager<List<Contact>>();
Contact conobj = new Contact();
List<Contact> lstContact = new List<Contact>()
{
new Contact(){ Name="abc", Age=14, Date=new DateTime(2017,05,12),         City="sss"},
};
lstContact.Add(conobj);
dbObj.SaveFile(lstContact);
dbObj.LoadFile();
Program callobj = new Program();
callobj.Menu();
Console.ReadLine();
}
public void Menu()
{
Console.WriteLine("Enter your Choice:");
Console.WriteLine("1. Edit Users");
    try
        {
            int i = int.Parse(System.Console.ReadLine());
            switch (i)
            {
                case 1:
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        System.Console.WriteLine("You have selected to Edit Users");
                        Program.Edit();
                        break;
                    }
           }
        }
        catch
        { }
    }

    public static Contact Edit()
    {
        Console.Write("Enter Your Name: ");
        string Name = Console.ReadLine();

        Console.Write("Enter Your Age:");
        byte Age = byte.Parse(Console.ReadLine());

        Console.Write("Enter Your Date Of Birth in MM-DD-YYYY Format:");
        DateTime DOB = Convert.ToDateTime(Console.ReadLine());

        Console.Write("Enter Your City : ");
        string City = Console.ReadLine();
        Contact newobj = new Contact(0, Name, Age, DOB, City);

        Console.WriteLine("Your name is : " + newobj.Name.ToString());
        Console.WriteLine("Your Age is : " +         Convert.ToInt32(newobj.Age).ToString());
        Console.WriteLine("Your DOB is : " +     newobj.Date.ToShortDateString());
        Console.WriteLine("Your City is : " + newobj.City.ToString());

        Console.WriteLine("Type Y to edit the userdata and N to return back to the MENU");
        bool confirmed = false;
        string Key;
        do
        {
            Console.Write("Please enter your option: ");
            Key = Console.ReadLine();
            Console.WriteLine("You entered, " + Key + " as your response!");

            ConsoleKey response;
            do
            {
                Console.Write("Are you sure you want to edit? [y/n] ");
                response = Console.ReadKey(false).Key;
                if (response != ConsoleKey.Enter)
                    Console.WriteLine();

            } while (response != ConsoleKey.Y && response != ConsoleKey.N);

            confirmed = response == ConsoleKey.Y;
        } while (!confirmed);
        Console.WriteLine("You chose {0}!", Key);
        Contact editobj = new Contact(0, Name, Age, DOB, City);
        return editobj;
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Threading.Tasks;
名称空间联系人管理
{
班级计划
{
静态void Main(字符串[]参数)
{
FileManager dbObj=newfilemanager();
触点conobj=新触点();
List lstContact=新列表()
{
新联系人(){Name=“abc”,年龄=14岁,日期=new DateTime(2017,05,12),City=“sss”},
};
lstContact.Add(conobj);
dbObj.SaveFile(lstContact);
dbObj.LoadFile();
Program callobj=新程序();
callobj.Menu();
Console.ReadLine();
}
公共无效菜单()
{
Console.WriteLine(“输入您的选择:”);
Console.WriteLine(“1.编辑用户”);
尝试
{
int i=int.Parse(System.Console.ReadLine());
开关(一)
{
案例1:
{
Console.ForegroundColor=ConsoleColor.Green;
System.Console.WriteLine(“您已选择编辑用户”);
Program.Edit();
打破
}
}
}
抓住
{ }
}
公共静态联系人编辑()
{
控制台。写(“输入您的姓名:”);
字符串名称=Console.ReadLine();
控制台。写(“输入您的年龄:”;
byte Age=byte.Parse(Console.ReadLine());
Console.Write(“以MM-DD-YYYY格式输入您的出生日期:”);
DateTime DOB=Convert.ToDateTime(Console.ReadLine());
控制台。写下(“进入你的城市:”;
字符串City=Console.ReadLine();
联系人newobj=新联系人(0,姓名,年龄,出生日期,城市);
WriteLine(“您的名字是:+newobj.name.ToString());
WriteLine(“您的年龄是:”+Convert.ToInt32(newobj.Age.ToString());
WriteLine(“您的DOB是:+newobj.Date.ToShortDateString());
Console.WriteLine(“您的城市是:+newobj.City.ToString());
WriteLine(“键入Y编辑用户数据,键入N返回菜单”);
布尔确认=假;
字符串键;
做
{
控制台。写(“请输入您的选项:”);
Key=Console.ReadLine();
WriteLine(“您输入,+Key+”作为您的响应!”);
康索尔基反应;
做
{
Console.Write(“您确定要编辑吗?[y/n]”);
response=Console.ReadKey(false).Key;
if(响应!=ConsoleKey.Enter)
Console.WriteLine();
}while(response!=ConsoleKey.Y&&response!=ConsoleKey.N);
确认=响应==ConsoleKey.Y;
}而(!已确认);
WriteLine(“您选择了{0}!”,Key);
联系人editobj=新联系人(0,姓名,年龄,出生日期,城市);
返回editobj;
}
}
}


我想从用户那个里输入数据,然后我想编辑那个些数据。能做到吗?我的意思是,是否可以编辑数据,然后在控制台应用程序中返回修改后的数据?另外,我想也许我可以在下面创建另一个对象,然后在编辑完成后,我可以返回新对象,它将覆盖先前创建的对象。我说得对吗?我希望我提出了正确的问题。请帮帮我。提前感谢。:)

是的,这是可以做到的,但不清楚你想要什么。给我们一个你想要的输入和输出的例子。你已经试过了,我想知道用户的姓名,年龄,城市,出生日期。然后我编写了打印数据的代码。现在我想编辑相同的数据,修改它并打印修改后的数据。您想如何编辑或修改它。给我们举一个例子,用例子数据;)你的意思是什么?是的,可以,但不清楚你想要什么。给我们一个你想要的输入和输出的例子。你已经试过了,我想知道用户的姓名,年龄,城市,出生日期。然后我编写了打印数据的代码。现在我想编辑相同的数据,修改它并打印修改后的数据。您想如何编辑或修改它。给我们举一个例子,用例子数据;)你是指类似的东西吗?