Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Read Text File and File Info in CSharp

In C#, using FileInfo and File classes, we can read a file meta data and read its text content. This example explains how to use FileInfo and File classes. 

 

Write the following code in the class "FileUtility.cs"

using System;
using System.IO;

namespace Com.Example.Csharp
{
    class FileUtility
    {
        public void GetFileInformation(String fileName)
        {
            FileInfo fileInfo=new FileInfo(fileName);
            Console.WriteLine("File created time : {0}", fileInfo.CreationTime);
            Console.WriteLine("File created UTC time : {0}", fileInfo.CreationTimeUtc);
            Console.WriteLine("File full path is: "+fileInfo.FullName);
            Console.WriteLine("File last write time {0}",fileInfo.LastWriteTime);
            Console.WriteLine("File last write time UTC  " +fileInfo.LastWriteTimeUtc);
            Console.WriteLine("File last access time {0} ",fileInfo.LastAccessTime);
            Console.WriteLine("File last access time UTC  {0} ",fileInfo.LastWriteTimeUtc);
        }

        public void ReadFileContent(String fileName)
        {
            var content = File.ReadAllText(fileName);
            Console.WriteLine(content);
        }
    }
}

Write the following code in the main Program file "Program.cs"

using System;main

namespace Com.Example.Csharp
{
    class Program
    {
        private static void Main(string[] args)
        {
            //Read file meta data
            FileUtility fileUtility=new FileUtility();
            fileUtility.GetFileInformation("test_txt.txt");
            Console.ReadKey();
            //Read file content
            fileUtility.ReadFileContent("test_txt.txt");
            Console.ReadKey();
        }
    }
}

Run the program and see the result displayed on the console output.

File created time : 6/29/2015 9:48:04 PM
File created UTC time : 6/30/2015 1:48:04 AM
File full path is: C:\*************\bin\Debug\test_txt.txt
File last write time 7/6/2015 8:47:46 PM
File last write time UTC  7/7/2015 12:47:46 AM
File last access time 6/29/2015 9:48:04 PM
File last access time UTC  7/7/2015 12:47:46 AM
This is a test
This is a test