在Console Application中编辑文本

时间:2017-05-03 06:53:45

标签: 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<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;
    }
}

}

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

0 个答案:

没有答案