C# Program to Illustrate Methods of FileInfo Class

This is a C# Program to illustrate methods of fileinfo class.

Problem Description

This C# Program Illustrates Methods of FileInfo Class.

Problem Solution

Here use the FileInfo class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files.

Program/Source Code

Here is source code of the C# Program to Illustrate Methods of FileInfo Class. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate Methods of FileInfo Class
 */
using System;
using System.IO;
class Test
{
    public static void Main()
    {
        string path = Path.GetTempFileName();
        FileInfo fi1 = new FileInfo(path);
        using (StreamWriter sw = fi1.CreateText())
        {
            sw.WriteLine("This is");
            sw.WriteLine("Sanfoundry");
            sw.WriteLine("Website");
        }
        using (StreamReader sr = fi1.OpenText())
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
        try
        {
            string path2 = Path.GetTempFileName();
            FileInfo fi2 = new FileInfo(path2);
            fi2.Delete();
            fi1.CopyTo(path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);
            fi2.Delete();
            Console.WriteLine("{0} was successfully deleted.", path2);
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        Console.Read();
    }
}
Program Explanation

This C# program is used to illustrate methods of FileInfo class. Using Path.GetTempFileName() function, we are getting a uniquely named, zero-byte temporary file on disk and return the full path of that file.

advertisement
advertisement

The FileInfo class is used for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files. Using try and catch, an error message is displayed when the error occurs.

Runtime Test Cases
 
This is 
Sanfoundry
Website.
C:\Users\win7\AppData\Local\Temp\tmpAEF8.tmp was copied to 
C:\users\Win7\AppData\Local\Temp\tmpAEF7.tmp
C:\users\Win7\AppData\Local\Temp\tmpAEF8.tmp was successfully deleted.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.