Write (Save) Console Application output in Notepad using C#

Last Reply one month ago By dharmendr

Posted one month ago

I created a registration form in console application and output in notepad.

But the code is not standardised. How to make it ?

The output is perfect but code.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int partner = 0;
            FileStream ostrm;
            StreamWriter writer;
            TextWriter oldout = Console.Out;
            string codex = "";
            string code1x = "";
            string code2x = "";
            string code21x = "";
            string code3x = "";
            int key;
            string first = "Enter First Name";
            string second = "Enter surName";
            string third = "Enter Date Of Birth,(dd/MM/yyyy)";
            string fourth = "Enter Martial Status(s/m)";
            String five = "Age";
            Console.WriteLine(first);
            Console.WriteLine(" ");
            string code = Console.ReadLine();
            Console.WriteLine(second);
            string code1 = Console.ReadLine();
            Console.WriteLine(third);
            string code2 = Console.ReadLine();
            DateTime dob = Convert.ToDateTime(code2);
            string code21 = Console.ReadLine();
            Console.WriteLine(CalculateYourAge(dob));
            string Age = CalculateYourAge(dob);
            int ageyr = int.Parse(Age.ToString());
            Console.WriteLine(fourth);
            string singleormarried = Console.ReadLine();

            string code3 = Console.ReadLine();
            Console.WriteLine();

            if (ageyr < 18)
            {
                Console.WriteLine("Parent Autorisation Needed,do you?(y/n");
                string authorisation = Console.ReadLine();
                if ((authorisation == "n") || (authorisation == "N"))
                {
                    Console.Write("Access Denied");
                    string remark = Console.ReadLine();
                }
            }
            if((singleormarried=="m"))
            {
                partner = 1;
                string pfirst = "Enter Partner's First Name";
                string psecond = "Enter surName";
                string pthird = "Enter Date Of Birth,(dd/MM/yyyy)";
            
                Console.WriteLine(pfirst);
                Console.WriteLine(" ");
                codex = Console.ReadLine();
                Console.WriteLine(psecond);
                code1x = Console.ReadLine();
                Console.WriteLine(pthird);
                code2x = Console.ReadLine();
                DateTime dobx = Convert.ToDateTime(code2x);
                code21x = Console.ReadLine();
                Console.WriteLine(CalculateYourAge(dobx));
                string Agex = CalculateYourAge(dobx);

                int ageyrx = int.Parse(Age.ToString());
             
                Console.WriteLine();
            }
                string line = first + "  " + code;
                string line1 = second + "  " + code1;
                string line2 = third + "  " + code2;
                string age = five+" "+Age;
                string line3 = fourth + "  " + code3;



                string linex = first + "  " + codex;
                string line1x = second + "  " + code1x;
                string line2x = third + "  " + code2x;


                string s = "***********************   Registration Form ************************" + Environment.NewLine + Environment.NewLine + line + Environment.NewLine + Environment.NewLine + line1 + Environment.NewLine + Environment.NewLine + line2  +Environment.NewLine+Environment.NewLine + age +  Environment.NewLine +Environment.NewLine + line3;
                string s1 = "";
                if (partner == 1)
                {
                    s1 = Environment.NewLine + Environment.NewLine + "***********************   Partner's Name ************************" + Environment.NewLine + Environment.NewLine + linex + Environment.NewLine + Environment.NewLine + line1x + Environment.NewLine + Environment.NewLine + line2x;
                }
                else
                {
                    s1 = "";
                }
                string s2 = s + s1;
                FileStream filestream = new FileStream("D:\\people.txt", FileMode.Create);
                StreamReader sr = new StreamReader(filestream);
                sr.Close();
                System.IO.File.WriteAllText("D:\\people.txt", s2);
        }
        public static string CalculateYourAge(DateTime dob)
        {
            var now = DateTime.Now;
            var years = new DateTime(DateTime.Now.Subtract(dob).Ticks).Year - 1;
            var pastYearDate = dob.AddYears(years);
            var months = 0;
            for (var i = 1; i <= 12; i++)
            {
                if (pastYearDate.AddMonths(i) == now)
                {
                    months = i;
                    break;
                }

                if (pastYearDate.AddMonths(i) < now)
                    continue;
                months = i - 1;
                break;
            }

            var days = now.Subtract(pastYearDate.AddMonths(months)).Days;
            var hours = now.Subtract(pastYearDate).Hours;
            var seconds = now.Subtract(pastYearDate).Seconds;
            return string.Format("{0}", years);
            // return string.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s) {3} Hour(s) {4} Second(s)", years, months, days, hours, seconds);
        }
    }
}

 

 


Posted one month ago

Is there a better way to get output in notepad

all at  once from console application.


Posted one month ago

Your code is correct.

There is nothing required to be modified as you have already created separated method to calculate the Age.