Sunday, 20 May 2018

How to Create folder Directory in C#





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;



namespace ConsoleApplication25

{

    class Program

    {

        static void Main(string[] args)

        {



            l1:

            Console.WriteLine("Enter the folder name :");

            string n = Console.ReadLine();

            DirectoryInfo DIR = new DirectoryInfo(@"C:\Users\salman\Desktop\FILE\"+n);





            try

            {



                if (DIR.Exists)

                {

                    Console.WriteLine("THIS FOLDER ALREADY EXIST......\nPress any key to go back");

                    Console.ReadLine();

                    Console.Clear();

                    goto l1;

                }

                else

                {

                    DIR.Create();

                    Console.Write("fOLDER CREATED.....");

                }





            }

            catch (Exception E)

            {

                Console.ForegroundColor = System.ConsoleColor.Red;

                Console.WriteLine("Folder couldnot be created due to " + E);

            }



            Console.WriteLine("Enter the folder that u want to delete.......");

            n = Console.ReadLine();

            new DirectoryInfo(@"C:\Users\salman\Desktop\FILE\" + n);

            DIR.Delete();

            Console.WriteLine("Folder deleted.....");

            Console.ReadLine();



        }

    }

}


Text Reader and Text Writer in C# File Read Write Operation







-------------------------------------------------------------------------------------------------------------

Text WRITER IN C#

-------------------------------------------------------------------------------------------------------------

         

 using (TextWriter wr=File.CreateText(@"C:\Users\salman\Desktop\FILE\textwriter.txt"))

            {



                for (int i = 0; i < 100; i++)

                {

                    wr.WriteLine((i+1)+"SALMAN MASOOD THE CONCEPT ACADEMY.....");

                }







            }

            Console.WriteLine("FILE CREATED SUCCESSFULLY....");


-------------------------------------------------------------------------------------------------------------

Text READER IN C#

-------------------------------------------------------------------------------------------------------------

  using (TextReader tr = File.OpenText(@"C:\Users\salman\Desktop\FILE\textwriter.txt"))

            {

                Console.WriteLine(tr.ReadToEnd());



            }

            Console.ReadLine();

Stream Writer and Stream Reader in C# File READ WIRTE OPERATIONS





-------------------------------------------------------------------------------------------------------------
FileStream Writer : Code
------------------------------------------------------------------------------------------------------------


 FileStream f = new FileStream(@"C:\Users\salman\Desktop\FILE\streamwriter.txt", FileMode.Open);

            StreamReader r = new StreamReader(f);

            string msg = r.ReadLine();

            Console.ForegroundColor = System.ConsoleColor.Red;



            Console.WriteLine("-------------------------------------------");

         

            Console.ForegroundColor = System.ConsoleColor.Green;





            Console.WriteLine(msg);



            Console.ForegroundColor = System.ConsoleColor.Red;



            Console.WriteLine("-------------------------------------------");

            f.Close();

         

            Console.ReadLine();

-------------------------------------------------------------------------------------------------------------
FileStream Reader : Code
------------------------------------------------------------------------------------------------------------

FileStream f = new FileStream(@"C:\Users\salman\Desktop\FILE\streamwriter.txt", FileMode.Create);
            StreamWriter s = new StreamWriter(f);
            Console.WriteLine("Enter your text: ");
            string msg = Console.ReadLine();

            s.Write(msg);
            s.Close();
            f.Close();
            Console.WriteLine("File created successfully.............");
            Console.ReadLine();



Thursday, 17 May 2018

File Stream in C# (How to read /write a text file in C#)





using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication21

{

    class Program

    {

        static void Main(string[] args)

        {

            FileStream F = new FileStream(@"C:\Users\salman\Desktop\FILE\salman.txt",FileMode.OpenOrCreate);

            Console.WriteLine("Enter the text :");

            string s = Console.ReadLine();

            byte[] ascii = Encoding.ASCII.GetBytes(s);

            foreach (byte item in ascii)

            {

                F.WriteByte(item);

            }



            Console.WriteLine("Data successfully written in file named as salman.txt");



            F.Close();





            Console.WriteLine("Press any key to read the same file............");

            Console.ReadKey();

            FileStream F2 = new FileStream(@"C:\Users\salman\Desktop\FILE\salman.txt", FileMode.OpenOrCreate);

            Console.ForegroundColor = System.ConsoleColor.Green;

            int i = 0;

            while ((i=F2.ReadByte())!=-1)

            {

                Console.Write((char)i);

            }

            Console.ReadKey();

            F2.Close();

       



        }

    }

}


Properties in C#







using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication20

{

    class Program

    {

        static void Main(string[] args)

        {

            EMPLOYEE EMP = new EMPLOYEE();

            Console.WriteLine("Enter the ID: ");

            EMP.ID = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the name ");

            EMP.NAME = Console.ReadLine();

            Console.WriteLine("Enter the salary ");

            EMP.SALARY = float.Parse(Console.ReadLine());



            EMP.display();

     

        }

    }

}
-------------------------------------------------------------------------------------------

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

namespace ConsoleApplication20
{
    class EMPLOYEE
    {
        public int ID { get; set; }
        public string NAME { get; set; }

        public float SALARY { get; set; }
   
    public void display()
        {

            Console.WriteLine("ID: "+ID);
            Console.WriteLine("Name "+NAME);
            Console.WriteLine("Salary: "+SALARY);

        }
   
    }
}


struct in C#



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication19

{

    class Program

    {

        static void Main(string[] args)

        {



            Rectangle r = new Rectangle(4,3);

            r.displayarea();

            Console.ReadLine();

        }

    }



    public struct Rectangle

    {

        int w, h;

        public Rectangle(int h, int w)

        {

            this.h = h;

            this.w = w;

        }



        public void displayarea()

        {



            Console.WriteLine("Area of Rectangle is "+(w*h));

        }



    }

}


Wednesday, 16 May 2018

Static Constructor in C#





STUDENT CLASS:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication18.Model

{

    class student

    {



        int id;

        string name;

        static string collegename;

        public student(int id,string name)

        {

            this.id = id;

            this.name = name;

        }

        static student()

        {

            collegename = "Govt Dehli College";

        }



        public void display()

        {

           

            Console.ForegroundColor = System.ConsoleColor.Green;

            Console.WriteLine("************************************");

            Console.ForegroundColor = System.ConsoleColor.Magenta;

            Console.WriteLine("EnrolmentID: "+id);

            Console.WriteLine("Name : "+name);

            Console.WriteLine("College: "+collegename);



            Console.ForegroundColor = System.ConsoleColor.Green;

            Console.WriteLine("************************************");



        }

    }

}

MAIN CLASS:
using ConsoleApplication18.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            student[] s = new student[]
            {
                new student(123,"salman"),
                 new student(124,"ali"),
                  new student(125,"sana"), 
                  new student(126,"kashif"),
            };

            foreach (student item in s)
            {
                item.display();
            }


        }
    }
}


Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...