Module 14 - Working With Files in C#
Module 14 - Working With Files in C#
By
SRIRAM . B
Overview
GetCreationTime, GetLastAccessTime,
GetLastWriteTime (All returns an object of
DateTime Struct)
DirectoryInfo members
Create, Delete, GetDirectories, GetFiles
Streams
In .Net Framework input and output is done through streams.
Streams provide a way to write and read bytes to and from a storage medium.
A data channel having two ends, one is attached to the data source while the
other is attached to the reader or writer of the data.
Streams..
Streams..
Streams can be used for:
Sequential file access
Read from and write to the beginning of a file
Random file access
Read from and write to any locations within a file
This opens the file, Myfile.txt , in the append mode for the
write operation.
Example – File Stream
using System;
using System.IO;
namespace Files
{
class Class1
{
static void Main(string[] args)
{
string str;
try
{
FileStream fs = new FileStream(@"c:\newfile.xls",
FileMode.OpenOrCreate);
Example – File Stream
}
}
}
Stream Writer
The objects of StreamWriter class are used to
perform write operations to a stream.
Example:
StreamWriter sw=new StreamWriter(fs);
In this code, fs is an object of the stream class.
Example – Stream Writer
using System;
using System.IO;
namespace day8
{
class Class1
{
static void Main(string[] args)
{
try
{
FileStream fs =
new FileStream(@"d:\log.txt", FileMode.OpenOrCreate);
Example – Stream Writer..
namespace day8
{
class Class1
{
static void Main(string[] args)
{
string str;
try
{
FileStream fs = new
FileStream(@"d:\log.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
str = sr.ReadLine();
Example – Stream Reader
}
}
}
}
Random Access
Reading data using File Stream class is not as easy
the StreamReader class because it deals exclusively
with Raw bytes.
fs.Seek(0, SeekOrigin.Begin);
fs.Write(bdata, 0, bdata.Length);
fs.Flush();
}
catch (IOException e)
{
Console.WriteLine("An IO exception occurred" + e);
}
}
Example – Random Access
fs.Seek(0, SeekOrigin.Begin);
System.IAsyncResult
synchResult = fs.BeginRead(bytedata, 0,100,null,null);
catch(IOException e)
{
Console.WriteLine(“Exception Occurred”+e.ToString());
}
}
}
}
Multi Tasking & MultiThreading
MultiTasking
Different applications execute at the same time
MultiThreading
One application has more than one application path at
the same time
Namespace to be included Using System.Threading
MultiThreading
Creating a Thread
The method should return void and not take any parameters.
MultiThreading..
A thread to be started using the Start method of the
Thread class.
Methods of Thread class
Thread.Sleep(int time in milliseconds)
Name (Property to get/set a thread’s name)
IsAlive(Property to find whether a thread is alive or not)
ThreadState (Property – Aborted, Stopped, Running,
Suspended, Unstarted etc)
Priority – (Property to set/get the priority of a thread)
Start
Abort
Suspend
Resume
Join
MultiThreading..
Thread Priorities
– Normal, AboveNormal, BelowNormal, Highest and Lowest
– Default priority is Normal
– To change the priority
» secondThread.Priority = ThreadPriority.AboveNormal;
When a thread is aborted a ThreadAbortException is thrown.