c sharp file handling-Final-2.0

Views:
 
Category: Education
     
 

Presentation Description

File handling in C#

Comments

Presentation Transcript

File Handling In C# : 

File Handling In C# Mohammed Arif(01)

File Handling –C# : 

File Handling –C# Variables and arrays only temporary - lost when a program terminates “Files” used for long term(persistant) storage We will cover: File types File and Directory classes File Reading and Writing(Program Demo) Search directory(Program Demo) Sequential-access files Random-access files

File Types – Text (ASCII) and Binary : 

File Types – Text (ASCII) and Binary Text files - ASCII characters with CR and LF(Carriage Return/Linefeed). Handled in a line-by-line, or sequential manner. Binary Files - Typed and Untyped files Binary files all values - can’t be displayed - .EXE file. Typed files - known format using data types - database. Untyped files - no rigid structure, sizes can vary. E.g. EXE program files, BMP bitmap graphic files. Program using it knows how to deal with it.

Files and streams : 

Files and streams C# views files as a sequential stream of bytes. File ends with EOF marker or after a byte count. You open the file, read and write to it, then close it. Uses StreamReader and StreamWriter Classes They implement methods to read and write files. Similar classes used to read/write to Internet or serial I/O

File Handling Classes : 

File Handling Classes DriveInfo Class: DriveInfo class is used to get information about computers system. This class models a drive and provides methods and properties to query for drive information(Provides access to information on a drive). Directory Class: Exposes static methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited. Used to do all directory manipulation. DirectoryInfo Class: Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited. When we use DirectoryInfo class If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of the Directory class. File Namespace :Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. The File namespace presents you with a number of useful methods[mentioned later…] FileInfo Class: Provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. This class cannot be inherited.

DriveInfo Class: : 

DriveInfo Class: 6

Directory Class : 

Directory Class 7

DirectoryInfo Class : implements the FileSystemInfo interface. : 

DirectoryInfo Class : implements the FileSystemInfo interface. 8

File Namespace : 

File Namespace 9

File Namespace (Continued..) : 

File Namespace (Continued..) 10

FileInfo Class : 

FileInfo Class 11

FileInfo Class (Continued..) : 

FileInfo Class (Continued..) 12

File Handling –C# : 

File Handling –C# Binary files: // Read: byte[] fileByteArray = new byte[1000]; fileByteArray = File.ReadAllBytes("test.bin"); // To Write: File.WriteAllBytes("test.bin", fileByteArray); 13

Sequential Files : 

Sequential Files The process of continually adding new data to the end of an existing file creates what is called a sequential file . With sequential files, new data is added on to the end of the file. There are no gaps in the data. Indeed, one of the advantages of sequential files is that they are dense. That is, every byte of storage space in a sequential file is filled with a piece of information. Ex: Sequential files are like the old cassette music tapes

Advantages and Disadvantages of Sequential Files : 

Advantages and Disadvantages of Sequential Files Advantages: files are dense: they waste no disk space Sequential files are often used for information that rarely requires editing or updating. E - mail messages,error logs, documents, letters etc. Disadvantages: you are forced to read through unwanted data to get to the data you actually want to use. editing the information in a sequential file is awkward at best(in many cases: editing some part of a sequential file means reading the old file into memory, rewriting the old file information up to the point of the edit to a new file, writing the new information to the new file, and then appending the remaining part of the old file onto the new file. ) Sequential Files are not used where information needs to be updated frequently

Random Access Files : 

Random Access Files Allows instant access to information Individual records accessed without searching through file. Easiest when all records are a fixed length. Data can be inserted without destroying other data in file Records can be updated without rewriting the entire file Uses file-position pointer - points to next byte to be read or written - can be repositioned to any point in file 16

Advantages and Disadvantages of Random Access Files : 

Advantages and Disadvantages of Random Access Files Advantages: The biggest advantage of random access files is that you can get to a particular record much faster than you can with sequential files. Random access file makes locating an individual record very fast. Because each record in a random access file is exactly the same size, you can easily update a particular record and rewrite that record without having to copy the entire file. Record updating is much faster with random access files because you have to rewrite only the record that has been changed. Disadvantage: you must design the record size in terms of a worst - case scenario. Ex: Even though you know that most peoples ’ first names do not exceed 10 characters, you might have to design the number of bytes devoted to the customer ’ s first name to be 15 bytes There are information “ gaps ” in the file where no data are stored.

File Creation using (FileStream and StreamWriter) : 

File Creation using (FileStream and StreamWriter) using System; using System.IO; using System.Text; namespace file_read_write { class FileWrite { public static void Main(string[] args) { FileStream fs = new FileStream("c:\\test.txt", FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); Console.WriteLine("Enter the text which you want to write to the file"); string str = Console.ReadLine(); sw.WriteLine(str); sw.Flush(); sw.Close(); fs.Close(); } } }

File Creation using (FileStream and StreamWriter) .. : 

File created as:test.text In C:\ directory File Creation using (FileStream and StreamWriter) ..

File Reading using (FileStream and StreamReader) : 

File Reading using (FileStream and StreamReader) using System; using System.IO; using System.Text; namespace file_read_write { class FileRead { public static void Main(string[] args) { FileStream fs = new FileStream("c:\\test.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); Console.WriteLine("Program to show content of test file"); String buff = sr.ReadToEnd(); Console.WriteLine(buff); Console.ReadLine(); sr.Close(); fs.Close(); } } }

File Reading using (FileStream and StreamReader) .. : 

File Read as :test.text In C:\ directory File Reading using (FileStream and StreamReader) ..

C# Program to list the directories in a drive selected by user.(Mumbai University-AWT-2010) : 

C# Program to list the directories in a drive selected by user.(Mumbai University-AWT-2010) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; namespace WindowsFormsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { string str2 = listBox1.SelectedItem.ToString(); listBox1.Items.Clear(); string str1 = comboBox1.SelectedItem.ToString(); string str3 = str1 + str2; DriveInfo di = new DriveInfo(comboBox1.SelectedItem.ToString()); DirectoryInfo root = new DirectoryInfo(str1+str2); DirectoryInfo[] dir = root.GetDirectories(); foreach (DirectoryInfo d in dir) { listBox1.Items.Add(d.Name); } }

C# Program to list the directories in a drive selected by user.(Mumbai University-AWT-2010).. : 

C# Program to list the directories in a drive selected by user.(Mumbai University-AWT-2010).. private void Form1_Load(object sender, EventArgs e) { DriveInfo[] di = DriveInfo.GetDrives(); foreach (DriveInfo itemDrive in di) { comboBox1.Items.Add(itemDrive.Name); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { listBox1.Items.Clear(); DriveInfo di = new DriveInfo(comboBox1.SelectedItem.ToString()); DirectoryInfo root = new DirectoryInfo(comboBox1.SelectedItem.ToString()); DirectoryInfo[] dir = root.GetDirectories(); foreach (DirectoryInfo d in dir) { listBox1.Items.Add(d.Name); } MessageBox.Show("Available Free Space: " + di.AvailableFreeSpace + "\n" + "Drive Format: " + di.DriveFormat + "\n" + "Drive Type: " + di.DriveType + "\n" + "Is Ready: " + di.IsReady.ToString() + "\n" + "Name: " + di.Name + "\n" + "Root Directory: " + di.RootDirectory + "\n" + "Total Free Space: " + di.TotalFreeSpace + "\n" + "Total Size: " + di.TotalSize + "\n" + "Volume Label: " + di.VolumeLabel.ToString(), di.Name + " DRIVE INFORMATION"); } } }

C# Program to list the directories in a drive selected by user.(Mumbai University-AWT-2010).. : 

C# Program to list the directories in a drive selected by user.(Mumbai University-AWT-2010)..

C# Program to list the directories in a drive selected by user.(Mumbai University-AWT-2010).. : 

C# Program to list the directories in a drive selected by user.(Mumbai University-AWT-2010)..

Slide 26: 

Any Question

Thank U : 

Thank U

authorStream Live Help