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.

advertisement
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

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.

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

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