w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Create a file and move the file into the same folder to another name

Secondary Nav

C# Sharp File Handling : Exercise-10 with Solution

Write a program in C# Sharp to create a file and move the file into the same directory to another name.
Expected Output :

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 moved 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 

C# Sharp Code:

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

        
public class SimpleFileMove
{
    static void Main()
    {

        string sfileName = @"mytest.txt";
        string tfileName = @"mynewtest.txt";   	
	
/*      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 */

            if (File.Exists(sfileName))
            {
                File.Delete(sfileName);
            }
			if (File.Exists(tfileName))
            {
                File.Delete(tfileName);
            }
			Console.Write("\n\n Create a file and move the file in same folder to another name  :\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("");
            }        		
        System.IO.File.Move(sfileName, tfileName); // move a file to another name in same location:
        Console.WriteLine(" The file {0} successfully moved 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();		
    }
}

C# Sharp Practice online:


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

        
public class SimpleFileMove
{
    static void Main()
    {

        string sfileName = @"mytest.txt";
        string tfileName = @"mynewtest.txt";   	
	
/*      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 */

            if (File.Exists(sfileName))
            {
                File.Delete(sfileName);
            }
			if (File.Exists(tfileName))
            {
                File.Delete(tfileName);
            }
			Console.Write("\n\n Create a file and move the file in same folder to another name  :\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("");
            }        		
        System.IO.File.Move(sfileName, tfileName); // move a file to another name in same location:
        Console.WriteLine(" The file {0} successfully moved 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();		
    }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program