This is a C# Program to illustrate methods of fileinfo class.
This C# Program Illustrates Methods of FileInfo Class.
Here use the FileInfo class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files.
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(); } }
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.
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.
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.
- Apply for Computer Science Internship
- Check Computer Science Books
- Check C# Books
- Practice MCA MCQs
- Practice Computer Science MCQs