Showing posts with label File Info. Show all posts
Showing posts with label File Info. Show all posts

Sunday, March 9, 2014

Get StreamWriter in C#

DriveInfo , DirectoryInfo , FileInfoStreamReader , StreamWriter

StreamWriter in FileInfo

  • To write / read the content to / write the file, you require file streams.
  • A file stream acts as a pointer for the file, which contains the memory address of the file on the disc.
There are two types of file streams.
1)  Reading Streams
2)  Writing Streams
API: System.IO.StreamWriter (to write the content into the file)

Writing content to the file
Import the API
using System.IO;
Create the stream writer object
StreamWriter sw = new StreamWriter(file name);
Write the content
sw.Write(content here);
Close the Writer
sw.Close();

Program for StreamWriter                                                                

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileWriteDemo
{
class Program
{
static void Main(string[] args)
{
string filepath;
Console.WriteLine("Enter the file path:");
filepath = Console.ReadLine();
FileInfo fobj = new FileInfo(filepath);
if (!fobj.Exists)
{
string content;
Console.WriteLine("\nEnter content to write:");
content = Console.ReadLine();
StreamWriter sw = new StreamWriter(filepath);
sw.Write(content);
sw.Close();
Console.WriteLine("\nWritten successfully!");
}
else
Console.WriteLine("File already exists."); 
Console.Read();
}
}
}
DriveInfo , DirectoryInfo , FileInfoStreamReader , StreamWriter

Get FileInfo in C#

FileInfo Class

  • Similar to the drives and folders, the System.IO namespace is used to manipulate the files also.
  • The file manipulations include with checking for the file existence, get the file details like file  size,  file  attributes,  created  date,  last  accessed  date  etc.,  and  write  new  content  to the files and also read content from existing files.
  • Every file is to be represented as an object, created for System.IO.FileInfo class.

          Library: System.IO.FileInfo
    This class object represents a file on the file system. This able to get the information of the file and also to perform certain operations on that folder.

Object Construction:
  Syn: FileInfo obj = new FileInfo(path of the file);
  Ex:  FileInfo obj = new FileInfo(c:\\tc\list.h);
You can observe the list of all available properties, methods of this class.

Program for FileInfo                                                                         

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileInfoDemo
{
class Program
{
static void Main(string[] args)
{
string filepath;
Console.WriteLine("Enter the file path:");
filepath = Console.ReadLine();
FileInfo fobj = new FileInfo(filepath);
if (fobj.Exists)
{
Console.WriteLine("\nName: " + fobj.Name);
Console.WriteLine("Full Name: " + fobj.FullName);
Console.WriteLine("Extension: " + fobj.Extension);
Console.WriteLine("Directory: " + fobj.DirectoryName);
Console.WriteLine("File Size: " + fobj.Length + " bytes");
Console.WriteLine("Created on: " + fobj.CreationTime);
Console.WriteLine("Last Accessed on: " + fobj.LastAccessTime);
Console.WriteLine("Last Modified on: " + fobj.LastWriteTime);
}
else
Console.WriteLine("File not found.");
Console.Read();
}
}
 DriveInfo , DirectoryInfo , FileInfo StreamReader , StreamWriter

Flag Counter