Professional Documents
Culture Documents
Chapter 4:
Manipulating Files
Write Text->File.WriteallText(filename,text)
Append Text->File.AppendallText(filename,text)
Read File->File.ReadallText(filename)
Move File->File.Move(from,to)
C# compiled by Dagne W 2
Objective
You will able to:
Move File->File.Move(from,to)
Replace>File.Replace(sfilename,dfilename,bfilename)
Encrypt File->File.Encrypt(filename)
Decrypt File->File.Decrypt(filename)
Delete File->File.Delete(filename)
C# compiled by Dagne W 3
Introduction to file
A file is a collection of data stored on a disk or on
program.
C# compiled by Dagne W 4
cretatefile()
public void cretatefile()
{
File.CreateText("f1.ppt");
File.CreateText("f2.ppt");
File.CreateText("f1.doc");
File.CreateText("f2.doc");
File.CreateText("f1.pdf");
File.CreateText("f2.pdf");
Console.WriteLine("files wascreated
successfully");
Console.WriteLine("_________________");
} C# compiled by Dagne W 5
createdirectory()
public void createdirectory()
{
Directory.CreateDirectory("d1");
Directory.CreateDirectory("d2");
Directory.CreateDirectory("d3");
Directory.CreateDirectory("d4");
Console.WriteLine("directory was created
successfully");
Console.WriteLine("_________________");
}
C# compiled by Dagne W 6
redfile() and append()
public void redfile()
{
Console.WriteLine(File.ReadAllText(filename));
}
public void appendtofile()
{
File.AppendAllText(filename, "hey");
Console.WriteLine("append successfully");
Console.WriteLine("_________________");
} C# compiled by Dagne W 7
writetofile()
public void writetofile()
{
File.WriteAllText(filename,
@"A file is a collection of data stored on a disk or on
some other relatively permanent storage medium.
A file's existence does not depend on a running
program. ");
Console.WriteLine("file write successfully");
}
C# compiled by Dagne W 8
deletefile() and copyfile()
public void deletefile()
{
File.Delete(filename);
Console.WriteLine("file deleted successfully");
}
public void copyfile()
{
File.Copy(filename, "bc.doc");
}
C# compiled by Dagne W 9
encryptfile() and decryptedfiel()
public void encryptfile()
{
File.Encrypt(filename);
Console.WriteLine("file encrypted
successfully");
}
public void decryptfile()
{
File.Decrypt(filename);
}
C# compiled by Dagne W 10
movefile()
public void movefile()
{
string from = @"C:\Users\user\Documents\
Visual Studio 2015\Projects\filesss\filesss\bin\
Debug\it.doc";
string to = @"C:\Users\user\Desktop\it.doc";
File.Move(from,to);
Console.WriteLine("file moved successfully");
}
C# compiled by Dagne W 11
Reading and Writing Binary Files
A binary file is a sequence of bytes.
Unlike a text file, which is terminated by a special
end-of-file marker, a binary file consists of
nothing but data.
Steps involved in reading and writing binary files
are the same as for text files:
1. Connect a stream to the file.
2. Read or write the data, possibly using a loop.
3. Close the stream.
C# compiled by Dagne W 12
Reading Text file in C#
C# compiled by Dagne W 13
.
Writing Text file in C#
using System;
using System.IO;
public class FileStreamExample
{
public static void Main(string[] args)
{
FileStream f = new FileStream("e:\\
b.txt", FileMode.OpenOrCreate);//creating file stream
f.WriteByte(65);//writing byte into stream
f.Close();//closing stream
}
}
C# compiled by Dagne W 14
Reading byte file in C#
using System;
using System.IO;
public class FileStreamExample
{
public static void Main(string[] args)
{
FileStream f = new FileStream("e:\\b.txt", FileMode.OpenOrCreate);
int i = 0;
while ((i = f.ReadByte()) != -1)
{
Console.Write((char)i);
}
f.Close();
}
}
C# compiled by Dagne W 15
thank you!!!
C# compiled by Dagne W 16