Ejemplo n.º 1
0
    public static void Main()
    {
        string fileName = @"mytest.txt";

        try
        {
            // Delete the file if it exists.
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            Console.Write("\n\n Create a file with content in the disk :\n");
            Console.Write("---------------------------------------------\n");
            // Create the file.
            using (StreamWriter fileStr = File.CreateText(fileName))
            {
                fileStr.WriteLine(" Hello and Welcome");
                fileStr.WriteLine(" It is the first content");
                fileStr.WriteLine(" of the text file mytest.txt");
                Console.WriteLine(" A file created with content name mytest.txt\n\n");
            }
        }
        catch (Exception MyExcep)
        {
            Console.WriteLine(MyExcep.ToString());
        }
    }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            string fileName = "myArrayTest.txt";

            try
            {
                Console.Write("\n\n Create a file named {0} in the disk :\n", fileName);
                Console.Write("------------------------------------------------\n");
                // Create the file.

                using (var fileStr = File.AppendText(fileName))
                {
                    Console.WriteLine(" A file created with name {0}\n\n", fileName);

                    int[] numbers = new int[] { 8, 14, 54, 23, 11 };

                    fileStr.WriteLine("Array of numbers: \n");
                    foreach (int number in numbers)
                    {
                        fileStr.WriteLine(number);
                    }
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }

            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main()
        {
            string fileName = @"test.txt";

            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                Console.WriteLine("Append some text to an existing file");
                Console.WriteLine(" ");

                //Create a file

                using (StreamWriter fileStr = File.CreateText(fileName))
                {
                    fileStr.WriteLine("Hello!");
                    fileStr.WriteLine("How was your day?");
                }

                using (StreamReader sr = File.OpenText(fileName))
                {
                    string s = "";
                    Console.WriteLine("Here is the content of your file: ");

                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine();
                }
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"mytest.txt", true))
                {
                    file.WriteLine(" This is the line append at last line.");
                }
                using (StreamReader sr = File.OpenText(fileName))
                {
                    string s = "";
                    Console.WriteLine("Here is the content of the file after appending the text: ");
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine();
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
            Console.ReadKey();
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            var fileName = @"mytest.txt";

            try
            {
                // Delete the file if it exists.
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                Console.Write("\n\n Append some text to an existing file  :\n");
                Console.Write("--------------------------------------------\n");
                // Create the file.
                using (var fileStr = File.CreateText(fileName))
                {
                    fileStr.WriteLine(" Hello and Welcome");
                    fileStr.WriteLine(" It is the first content");
                    fileStr.WriteLine(" of the text file mytest.txt");
                }

                using (var sr = File.OpenText(fileName))
                {
                    var s = "";
                    Console.WriteLine(" Here is the content of the file mytest.txt : ");
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine("");
                }

                using (var file = new StreamWriter(@"mytest.txt", true))
                {
                    file.WriteLine(" This is the line appended at last line.");
                }

                using (var sr = File.OpenText(fileName))
                {
                    var s = "";
                    Console.WriteLine(" Here is the content of the file after appending the text : ");
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine("");
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            ///create a string variable to store the file

            string fileName = @"myTest.txt";

            //Check if the file is exists, if yes then delete it.
            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                //Display a message about what the program is doing
                Console.WriteLine("\n\nRead the first line from the file: \n");
                Console.WriteLine("-------------------------------------------");

                //Create the file by creating an instance of StreamWriter class and use WriteLine method in order to write 3 lines.
                using (StreamWriter filestr = File.CreateText(fileName))
                {
                    filestr.WriteLine(" test line 1");
                    filestr.WriteLine(" test line 2");
                    filestr.WriteLine(" test line 3");
                }
                //Create an instance of StreamReader to read from a file
                //The using statment also closes the StreamReader.
                using (StreamReader sr = File.OpenText(fileName))
                {//Read and display content from the file untill
                    //the end of the file is reached.
                    string s = " ";
                    Console.WriteLine("Here is the content of the file mytest.txt: ");
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine(" ");
                }
                //If the file exists read all the lines but display only the first line.
                Console.Write("\n the content of the first line is:  \n");
                if (File.Exists(fileName))
                {
                    string[] lines = File.ReadAllLines(fileName);
                    Console.Write(lines[0]);
                }
                Console.WriteLine();
            }
            //
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 6
0
        static void Main()
        {
            string fileName = @"mytest.txt";
            int    count;

            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                Console.WriteLine("Conter of lines in file.");
                Console.WriteLine("");

                //Create the file

                using (StreamWriter str = File.CreateText(fileName))
                {
                    str.WriteLine("This is first line.");
                    str.WriteLine("This is second line.");
                    str.WriteLine("This is third line.");
                    str.WriteLine("This is fourth line.");
                    str.WriteLine("This is fifth line.");
                }

                using (StreamReader openFile = File.OpenText(fileName))
                {
                    string s = "";

                    count = 0;

                    Console.WriteLine("The sum of lines in the file");

                    while ((s = openFile.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                        count++;
                    }
                    Console.WriteLine("");
                }
                Console.WriteLine("The number of lines in file {0} is: {1}", fileName, count);
            }

            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }

            Console.ReadKey();
        }
Ejemplo n.º 7
0
        public static void Main()
        {
            var fileName = @"mytest.txt";
            int count;

            try
            {
                // Delete the file if it exists.
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                Console.Write("\n\n Count the number of lines in a file :\n");
                Console.Write("------------------------------------------\n");
                // Create the file.
                using (var fileStr = File.CreateText(fileName))
                {
                    fileStr.WriteLine(" test line 1");
                    fileStr.WriteLine(" test line 2");
                    fileStr.WriteLine(" Test line 3");
                    fileStr.WriteLine(" test line 4");
                    fileStr.WriteLine(" test line 5");
                    fileStr.WriteLine(" Test line 6");
                }

                using (var sr = File.OpenText(fileName))
                {
                    var s = "";
                    count = 0;
                    Console.WriteLine(" Here is the content of the file mytest.txt : ");
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                        count++;
                    }

                    Console.WriteLine("");
                }

                Console.Write(" The number of lines in  the file {0} is : {1} \n\n", fileName, count);
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 8
0
        public static void Main()
        {
            var fileName = @"mytest.txt";

            try
            {
                // Delete the file if it exists.
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                Console.Write("\n\n Read the first line from a file  :\n");
                Console.Write("---------------------------------------\n");
                // Create the file.
                using (var fileStr = File.CreateText(fileName))
                {
                    fileStr.WriteLine(" test line 1");
                    fileStr.WriteLine(" test line 2");
                    fileStr.WriteLine(" Test line 3");
                }

                using (var sr = File.OpenText(fileName))
                {
                    var s = "";
                    Console.WriteLine(" Here is the content of the file mytest.txt : ");
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine("");
                }

                Console.Write("\n The content of the first line of the file is :\n");
                if (File.Exists(fileName))
                {
                    var lines = File.ReadAllLines(fileName);
                    Console.Write(lines[0]);
                }

                Console.WriteLine();
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 9
0
        public static void MainFH1()
        {
            string fileName = @"mytest.txt";

            try
            {
                Console.Write("\n\n Create a file named mytest.txt in the disk :\n");
                Console.Write("------------------------------------------------\n");
                // Create the file.
                using (FileStream fileStr = File.Create(fileName))
                {
                    Console.WriteLine(" A file created with name mytest.txt\n\n");
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
        //File handling:
        static void Exercise50()
        {
            //  Write a program in C# Sharp to create a blank file in the disk newly.
            string fileName = @"C:\Users\Admin\Desktop\Basics\mytest.txt";

            try
            {
                Console.Write("\n\n Create a file named mytest.txt in the disk :\n");
                Console.Write("------------------------------------------------\n");
                // Create the file.
                using (FileStream fileStr = File.Create(fileName))
                {
                    Console.WriteLine(" A file created with name mytest.txt\n\n");
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 11
0
        // 1
        public static void CreateBlankFile()
        {
            // C:\Users\tru55\Desktop\GitHub\Practice_April2020\File_Handling\
            string fileName = @"C:\Users\tru55\Desktop\GitHub\Practice_April2020\File_Handling\mytest2.txt";

            try
            {
                Console.WriteLine("Create a file named mytest2.txt in the disk");
                Console.WriteLine("------------------------------------------");
                // create the file
                using (FileStream fileStr = File.Create(fileName))
                {
                    Console.WriteLine("A file created with name mytest2.txt");
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 12
0
        public static void WriteFileName()
        {
            string fileName = @"mytest2.txt";

            try
            {
                File.Create(fileName);
                var          fs     = new FileStream(@"C:\mytest2.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs);
                writer.Write("Hi.. This is the sample text file you have created using FileStream Class.");
                writer.Close();
                Console.Write("\n\n Create a file named mytest.txt in the disk :\n");
                Console.Write("------------------------------------------------\n");
                File.Create(fileName);
                File.Create(@"D\:mytest.txt");
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 13
0
        static void Main()
        {
            string fileName = @"mytext.txt";

            try
            {
                //Delete file
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                Console.WriteLine("Create a new file with content.");
                Console.WriteLine("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-");

                //Create a file
                using (StreamWriter fileStr = File.CreateText(fileName))
                {
                    fileStr.WriteLine("Hello my friend!");
                    fileStr.WriteLine("This is the first context of this file.");
                }

                using (StreamReader sr = File.OpenText(fileName))
                {
                    string s = "";
                    Console.WriteLine("This is the context of {0}", fileName);

                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine();
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 14
0
        public static void Main()
        {
            var fileName = @"mytest.txt";

            try
            {
                // Delete the file if it exists.
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                Console.Write("\n\n Create a file with text and read the file  :\n");
                Console.Write("-------------------------------------------------\n");
                // Create the file.
                using (var fileStr = File.CreateText(fileName))
                {
                    fileStr.WriteLine(" Hello and Welcome");
                    fileStr.WriteLine(" It is the first content");
                    fileStr.WriteLine(" of the text file mytest.txt");
                }

                using (var sr = File.OpenText(fileName))
                {
                    var s = "";
                    Console.WriteLine(" Here is the content of the file mytest.txt : ");
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine("");
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 15
0
        static void Main()
        {
            string file = @"mytest.txt";

            try
            {
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                Console.WriteLine("The program creates a file with test and read the file");
                Console.WriteLine("----------------------------------------------------------");

                using (StreamWriter str = File.CreateText(file))
                {
                    str.WriteLine("Hello! ");
                    str.WriteLine("This is the content of this file...");
                }

                using (StreamReader streamReader = File.OpenText(file))
                {
                    string s = " ";
                    Console.WriteLine("Here is the content of my file {0}", file);
                    while ((s = streamReader.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                    Console.WriteLine(" ");
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
            Console.ReadKey();
        }
Ejemplo n.º 16
0
        public static void Main()
        {
            var fileName = @"mytest.txt";

            try
            {
                // Delete the file if it exists.
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                Console.Write("\n\n Create a file in the disk if it is exists:\n");
                Console.Write("-----------------------------------------------\n");
                // Create the file.
                using (var fileStr = File.Create(fileName))
                {
                    Console.WriteLine(" A file created with name mytest.txt\n\n");
                }
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }
Ejemplo n.º 17
0
        // Created method for gathering user IP and writing to text file
        static void WriteNetConfig()
        {
            // You can change the last component in parameter to add bat file to different location
            string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            // Console input for naming the file
            // The string "file" saves .bat to the end of the string so that when it is saved to the location of choice, it creates a functional .bat file
            Console.Write("Enter the name of the batch file you wish to create: ");
            string fileName = Console.ReadLine();
            string file     = fileName + ".bat";

            try
            {
                // Deleting file name if name already exisits
                if (File.Exists(Path.Combine(docPath, file)))
                {
                    File.Delete(Path.Combine(docPath, file));
                    Console.WriteLine();
                    Console.WriteLine(file + " file name already in use.");
                    Console.WriteLine("Deleting " + file + " and rewriting!");
                }

                // Writing net configuration (IPv4 address, subnet mask, default gateway, DNS servers) to batch file
                using (StreamWriter fileWrite = File.CreateText(Path.Combine(docPath, file)))
                {
                    // Gathering local IPv4 address
                    #region
                    string      ipAdd  = "";
                    string      userIP = "";
                    IPHostEntry host;
                    host = Dns.GetHostEntry(ipAdd);

                    foreach (IPAddress ip in host.AddressList)
                    {
                        if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            userIP = ip.ToString();
                        }
                    }
                    #endregion

                    // Gathering the subnet mask
                    #region
                    string             subnetMask    = "";
                    NetworkInterface[] netInterfaces = NetworkInterface.GetAllNetworkInterfaces();

                    foreach (NetworkInterface subMask in netInterfaces)
                    {
                        if (subMask.OperationalStatus == OperationalStatus.Up)
                        {
                            if (subMask.NetworkInterfaceType == NetworkInterfaceType.Loopback)
                            {
                                continue;
                            }

                            UnicastIPAddressInformationCollection UnicastIPInfo = subMask.GetIPProperties().UnicastAddresses;

                            foreach (UnicastIPAddressInformation subnet in UnicastIPInfo.Skip(1))
                            {
                                subnetMask = subnet.IPv4Mask.ToString();
                            }
                        }
                    }
                    #endregion

                    // Gathering the default gateway
                    #region
                    IPAddress defGateway = null;
                    var       netAdapt   = NetworkInterface.GetAllNetworkInterfaces();
                    if (netAdapt.Any())
                    {
                        foreach (var adapter in netAdapt)
                        {
                            var ipProper = adapter.GetIPProperties();

                            if (ipProper == null)
                            {
                                continue;
                            }

                            var gateway1 = ipProper.GatewayAddresses;

                            if (!gateway1.Any())
                            {
                                continue;
                            }

                            var gateway2 = gateway1.FirstOrDefault(g => g.Address.AddressFamily.ToString() == "InterNetwork");

                            if (gateway2 == null)
                            {
                                continue;
                            }

                            defGateway = gateway2.Address;
                        }
                    }
                    #endregion

                    // Gathering the DNS server information
                    #region
                    string primaryDNS   = "";
                    string alternateDNS = "";

                    foreach (NetworkInterface dnsServers in netInterfaces)
                    {
                        if (dnsServers.OperationalStatus == OperationalStatus.Up)
                        {
                            IPInterfaceProperties ipProperties = dnsServers.GetIPProperties();
                            IPAddressCollection   dnsAddresses = ipProperties.DnsAddresses;

                            // Returns the first address for DNS servers by using .Take
                            foreach (IPAddress dns in dnsAddresses.Take(1))
                            {
                                primaryDNS = dns.ToString();
                            }

                            // Returns the second address for DNS servers by using .Skip(1)
                            foreach (IPAddress dns in dnsAddresses.Skip(1))
                            {
                                alternateDNS = dns.ToString();
                            }
                            break;
                        }
                    }
                    #endregion

                    // Gathering the network adapter name to be included into the batch file
                    #region
                    string adapterName = "";

                    foreach (NetworkInterface netAdapter in netInterfaces)
                    {
                        // If check for getting just the currently connected adapters as well as excluding the tunnel and loopback adapters
                        if (netAdapter.OperationalStatus == OperationalStatus.Up && netAdapter.NetworkInterfaceType != NetworkInterfaceType.Tunnel && netAdapter.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                        {
                            adapterName = netAdapter.Name;
                        }
                    }
                    #endregion

                    // Writing the batch file script
                    fileWrite.WriteLine("@echo off");
                    fileWrite.WriteLine("netsh interface ipv4 set address name=\"" + adapterName + "\" static " + userIP + " " + subnetMask + " " + defGateway);
                    fileWrite.WriteLine();
                    fileWrite.WriteLine("@echo off");
                    fileWrite.WriteLine("netsh interface ipv4 set dns name=\"" + adapterName + "\" static " + primaryDNS + "");
                    fileWrite.WriteLine("netsh interface ipv4 add dns name=\"" + adapterName + "\" " + alternateDNS + " index=2");
                }

                // FInal console output
                Console.WriteLine();
                Console.WriteLine(file + " has successfully been created and added to user's desktop!");
                Console.WriteLine();
                Console.WriteLine("Press ENTER to exit....");
                Console.ReadLine();
            }
            catch (Exception MyExcep)
            {
                Console.WriteLine(MyExcep.ToString());
            }
        }