Sie sind auf Seite 1von 7

Stream

=========
MemoryStream class is used to read and write data to memory.
BufferedStream Class is used to read and write to the buffer.
FileStream Class is used to read and write operation on files.
NetworkStream Class is used to send and receive data across the network. It resi
des within System.Net.Sockets namespace.
CryptoStream is used for encrypting the data. System.It resides within the Syste
m.Security.Cryptography namespace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
MemoryStream mem = new MemoryStream();
BufferedStream buff = new BufferedStream(mem);
buff.WriteByte((byte)100);
buff.WriteByte((byte)200);
buff.WriteByte((byte)120);
buff.Position = 0;
byte[] Arr = { 1, 2, 3, 4 };
buff.Read(Arr, 0, 2);
for (int i = 0; i < 4; i++)
{
Console.WriteLine("the value is {0}", Arr[i]);
}
Console.WriteLine("The return type for ReadByte() is {0}", buff.Re
adByte());
Console.WriteLine("The return type for ReadByte() is {0}", buff.Re
adByte());
Console.ReadLine();
}
}
}
O/p
--the
the
the
The

value is 100
value is 2
value is 3
return type for ReadByte()

Read (
byte[] array,
int offset,
int count)
array
Type: System.Byte[]

is -1

The buffer to which bytes are to be copied.


offset
Type: System.Int32
The byte offset in the buffer at which to begin reading bytes
count
Type: System.Int32
The number of bytes to be read.
TextReader Class
================
TextReader is the abstract base class for the StreamReader and StringReader Clas
ses.
Methods
Read() - Reads a character from byte stream.
ReadLine() - Reads a line of character from the byte stream.
ReadToEnd() - reads byte stream from the current position to the end of the stre
am.
Peek - Returns next character available in the stream.
Seek: sets the current position of this stream to the given value
Peek: returns the next available charcter but does not consume it.

using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.IO;

namespace ConsoleApplication8
{
class StreamReaderExample
{
static void Main(string[] args)
{
Console.WriteLine("Enter the file name:");
string fileName = Console.ReadLine();
if (!File.Exists(fileName))
{
Console.WriteLine("{0} does not exist!", fileName);
//return;
}
StreamReader objStreamRead = File.OpenText(fileName);
String input;
while ((input = objStreamRead.ReadLine()) != null)
{
Console.WriteLine(input);
}
Console.WriteLine("The end of the strem is reached");
objStreamRead.Close();
Console.ReadLine();
}
}

TextWriter
==========
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication8
{
class StreamWriterExample
{
static void Main(string[] args)
{
Console.WriteLine("Enter the file name:");
string fileName = Console.ReadLine();
if (!File.Exists(fileName))
{
Console.WriteLine("{0} does not exist!", fileName);
return;
}
StreamWriter objStreamRead = File.AppendText(fileName);
Console.WriteLine("Enter a string to be written to the file:");
String input = Console.ReadLine();
objStreamRead.WriteLine(input);
// Console.WriteLine("The end of the strem is reached");
objStreamRead.Close();
StreamReader objread = File.OpenText(fileName);
String inn;
while ((inn = objread.ReadLine()) != null)
{
Console.WriteLine(inn);
}
Console.WriteLine("The end of the strem is reached");
objread.Close();
Console.ReadLine();
}
}
}
File Class
=============
Methods
-------Copy - This method is used to copy the contents of a source file to a destinatio
n file. If the destination file does not
exist, then a new file is create
d with the specified name in the specified path
Create - This method is used to create a file in the specified path
Delete - used to delete a file with the specified path
Exists - used to verify whether a file is specified name exists in the specified
path
Move - used to move the specified file from the source location to the destinati

on location.
GetAttributes - used to get the attributes of the file
GetCreationTime - used to get the creation date and the time of the specified fi
le or directory.

using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.IO;

namespace ConsoleApplication8
{
class FileStreamExample
{
static void Main(string[] args)
{
string filename = @"C:\Users\Bltuser\Desktop\Student.txt";
try
{
FileStream fs = File.Create(filename);
Byte[] info = new UTF8Encoding(true).GetBytes("This file was pro
gramatically created using C#");
fs.Write(info, 0, info.Length);
fs.Flush();
fs.Close();
Console.WriteLine(filename + " has been created");
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
}
}

Copy
====
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.IO;

namespace ConsoleApplication8
{
class FileStreamExample
{
static void Main(string[] args)
{
string filename = @"C:\Users\Bltuser\Desktop\Student.txt";
string filename2 = @"C:\Users\Bltuser\Desktop\abc.txt";
try

{
//Copy the files
File.Copy(filename, filename2);
Console.WriteLine("{0} has been copied to {1} ", filename, filen
ame2);
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}
Move
----using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.IO;

namespace ConsoleApplication8
{
class FileStreamExample
{
static void Main(string[] args)
{
string filename = @"C:\Users\Bltuser\Desktop\bhaskar.txt";
string filename2 = @"C:\Users\Bltuser\Documents\bbb.txt";
try
{
//Move the files
File.Move(filename, filename2); //For delete File.Move(filename,
filename2);
Console.WriteLine("{0} has been Moved to {1} ", filename, filena
me2);
File.Delete(filename);
Console.ReadLine();

//Delete

}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}

Directory Creation
=======================

using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.IO;

namespace ConsoleApplication8
{
class FileStreamExample
{
static void Main(string[] args)
{
Directory.CreateDirectory(@"C:\Users\Bltuser\Desktop\New folder3");
//Creating directory
Console.WriteLine("Directory created ! ");
Directory.CreateDirectory(@"C:\Users\Bltuser\Desktop\New folder3\bha
skar"); //creating SubDirectory
Console.WriteLine("subfolder created ! ");
Console.ReadLine();
}
}
}
List of files in the directory
================================
Console.WriteLine("List of files in this directory is:");
string[] arr = Directory.GetFiles(@"C:\Users\Bltuser\Desktop\aaa");
foreach (string i in arr)
{
Console.WriteLine(Path.GetFileName(i));
}
Console.ReadLine();
Display only one particular extension
=========================================
Method1:
-------string[] array2 = Directory.GetFiles(@"C:\Users\Bltuser\Desktop", "*.txt");
Console.WriteLine("----FILES----");
foreach (string name in array2)
{
Console.WriteLine(name);
}
Method2:
-------//retreive only particular file extension
Console.WriteLine("Retreive only particular file extension in this d
irectory is:");
string[] arr = Directory.GetFiles(@"C:\Users\Bltuser\Desktop");
foreach (string i in arr)

{
if (Path.GetExtension(i) == ".txt")
Console.WriteLine(Path.GetFileName(i));
}
Console.ReadLine();