Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Create a file and copy the file

C# Sharp File Handling: Exercise-9 with Solution

Write a program in C# Sharp to create and copy the file to another name and display the content.

Sample Solution:-

C# Sharp Code:

using System;
using System.IO;
using System.Text;


public class SimpleFileCopy
{
    static void Main()
    {
        string sfileName = @"mytest.txt";
        string tfileName = @"mynewtest.txt";
        
            // Delete the file if it exists.
            if (File.Exists(sfileName))
            {
                File.Delete(sfileName);
            }
			Console.Write("\n\n Create a file and copy the file  :\n");
			Console.Write("---------------------------------------\n");            
            // Create the file.
            using (StreamWriter fileStr = File.CreateText(sfileName)) 
            {
                fileStr.WriteLine(" Hello and Welcome");
                fileStr.WriteLine(" It is the first content");
                fileStr.WriteLine(" of the text file mytest.txt");
            }	            
            using (StreamReader sr = File.OpenText(sfileName))
            {
                string s = "";
				Console.WriteLine(" Here is the content of the file {0} : ",sfileName);
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("");
            }        
      
/*      string sourcefolder = "path";  // you can mention the path of source folder
		string targetfolder =  "path"; // you can mention the path of target folder 
		string sourceFile = System.IO.Path.Combine(sourcefolder, sfileName); // combine the source file with path
		string targetFile = System.IO.Path.Combine(targetfolder, tfileName);   // combine the target file with path */

/*		Create a new target folder if not exists
        if (!System.IO.Directory.Exists(targetfolder))
        {
            System.IO.Directory.CreateDirectory(targetfolder);
        }
		System.IO.File.Copy(sourceFile, destFile, true); // overwrite the target file if it already exists. */
        System.IO.File.Copy(sfileName, tfileName, true);
        Console.WriteLine(" The file {0} successfully copied to the name {1} in the same directory.",sfileName,tfileName );
        
using (StreamReader sr = File.OpenText(tfileName))
            {
                string s = "";
				Console.WriteLine(" Here is the content of the file {0} : ",tfileName);
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("");
            }       
        Console.ReadKey();
    }
}

Sample Output:

 Create a file and copy the file  :                                                                           
---------------------------------------                                                                       
 Here is the content of the file mytest.txt :                                                                 
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  
   
 The file mytest.txt successfully copied to the name mynewtest.txt in the same directory.                     
 Here is the content of the file mynewtest.txt :                                                              
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt

Flowchart :

Flowchart: C# Sharp Exercises - Create a file and copy the file.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to append some text to an existing file.
Next: Write a program in C# Sharp to create a file and move the file into the same directory to another name.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.