logo
  • Home
  • Rank
  • Tests
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C# Program to Illustrate Methods of FileInfo Class

Posted on September 23, 2013 by Manish

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.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.
« Prev Page - C# Program to Demonstrate Pass by Value Parameter
» Next Page - C# Program to Perform a Selection Sort

« C# Program to Demonstrate Pass by Value Parameter
C# Program to Implement PhoneBook »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Stacks & Queues
  2. C Programming Examples on Bitwise Operations
  3. C# Programming Examples on Threads
  4. C# Programming Examples on Delegates
  5. C# Programming Examples on Strings
  6. Java Programming Examples on Classes
  7. C# Programming Examples on Exceptions
  8. C# Programming Examples on Functions
  9. C# Programming Examples on Inheritance
  10. C# Programming Examples on Files
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and 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 & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.