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");
        using (StreamReader sr = fi1.OpenText())
            string s = "";
            while ((s = sr.ReadLine()) != null)
            string path2 = Path.GetTempFileName();
            FileInfo fi2 = new FileInfo(path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);
            Console.WriteLine("{0} was successfully deleted.", path2);
        catch (Exception e)
            Console.WriteLine("The process failed: {0}", e.ToString());
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: Join Sanfoundry@Linkedin

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 
C:\Users\win7\AppData\Local\Temp\tmpAEF8.tmp was copied to 
C:\users\Win7\AppData\Local\Temp\tmpAEF8.tmp was successfully deleted.

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

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

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!
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter