SEARCH C# PROGRAMMING CODES HERE

File Operations

File Operations in C#
In the last Section of Streams, we have learned how we can read and write files using SteamReader and StreamWriter classes, In this section, we will learn to deal with the File operations for Copying, Deleting and we will also create a program to check whether a particular file exists or not. In C# programming there is a wide range of array file activities. These activities incorporate opening a record, perusing or keeping in touch with a document. There can be occurrences wherein you need to work with documents authentically, in which case you would utilize the record tasks accessible in C#. A portion of the fundamental record activities is referenced below.

1-File.Copy
2-File.Delete
3-File.Exists

1-File Copy
To copy the preferred document we have to utilized to make a duplicate of a current document. In the first place, we are declaring a string variable called Path. This will be the Location and file of our @"d:\\File.txt record. This record will be the source document utilized for the duplicate task.
Next, we will declare a string variable called copypath. This will be the targeted location of another record called  @"e:\\File.txt document. This will be the goal document in which the substance will be composed of the source record File.txt.
once it will be done then finally we will call the File.Copy function to copy the document from this location@"d:\\File.txt  to the targeted location as @"e:\\File.txt.  Now simply copy the yellow highlighted code as follows to perform this task.

Program

using System;
using System.IO;

namespace program
{
    class Program
    {
        static void Main(string[] args)
        {
            String path = @"d:\\File.txt";
            String copypath = @"e:\\File.txt";
            File.Copy(path, copypath);
            Console.WriteLine("File copied SucessFully");
            Console.ReadKey();
   
        }
         
    }
 }
Output


2-File.Delete

This function is used to delete an existing document from the preferred location, to perform this task first we will declare a string variable called path which will be the location as @"e:\\File.txt of the file to delete which we created and have copied in the above program. Next, we will call the
File. Delete function to delete the file. Now simply copy the yellow highlighted code as follows to perform this task.

Program

using System;
using System.IO;

namespace program
{
    class Program
    {
        static void Main(string[] args)
        {
            String path = @"e:\File.txt";
            File.Delete(path);
            Console.WriteLine("File Deleted successfully");
            Console.ReadKey();
   
        }
         
    }
 }
Output



















3-File.Exists

File exists method is used to check if a particular file exists or not, To Implement this task first, we will set the string variable with the path as @"e:\\File.txt Next, and then we will call the File. Exists function to check if the file exists or not. If the File exists, a true value will be returned. but as we know that we have already deleted the file in the above program from this location @"e:\\File.txt so  file is not available so in this case will get the False value which means File not exists so to handel this situation we will create a program to display the message "File Exists" If the condition is True  else condition is false then we will display the message"File Does Not Exist",  Now simply copy the yellow highlighted code as follows to perform this task.

Program
using System;
using System.IO;

namespace program
{
    class Program
    {
        static void Main(string[] args)
        {
            String path = @"e:\\File.txt";

            if (File.Exists(path))

            {
                Console.WriteLine("File  Exists");  /*True condition*/
            }
            else
            {
                Console.WriteLine("File Does Not Exists"); /*False condition*/
            }
            Console.ReadKey();
      
        }
         
    }
 }


               Output

No comments:

Post a Comment