Saturday, 28 April 2012

Now use the BinaryReader and BinaryWriter classes to read and write primitive types to a file.The BinaryWriter writes primitive objects in their native format, so in order to read them using the BinaryReader, you must select the appropriate Read method.
The Example shows you how to do that; in this case, you are writing a value from a number of different primitive types to the text file and then reading the same value.

If you open this file in Notepad, you will see that the BinaryWriter has written the nonreadable binary data to the file. The BinaryReader provides a number of different methods for reading various kinds of Primitive types from the stream. In this Example, you use a different Read method for each primitive type that you Write to the file.

When you create a StreamReader, you must pass an existing stream instance as a constructor
parameter. The reader uses this stream as its underlying data source.
In this sample, you use the File class’s Open method to open a writable FileStream for your StreamWriter.

Also notice that you no longer have to deal with byte arrays. The StreamReader takes care of converting the data to a type that’s more user-friendly than a byte array. In this example, you are using the ReadToEnd method to read the entire stream and convert it to a string.The StreamReader provides a number of different methods for reading data that you can use depending on exactly how you want to read the data, from reading a single character using the Read method, to reading the entire file using the ReadToEnd method.

Thursday, 26 April 2012

There is a situation where you want to write data in Memory Stream.
You can write to the stream by encoding a string containing the information you want to write to a byte array and then using the stream’s Write method to write the byte array to the MemoryStreams.
The Close method also calls Flush internally to commit the data to the data store.

Wednesday, 25 April 2012

Any type of I/O operation you are performing in .NET, if you want to read or write data you eventually use a stream of some type. Streams are the basic mechanism. .NET uses to transfer data to and from its underlying source, it will be a file, communication pipe, or TCP/IP socket. The Stream class provides the basic functionality to read and write I/O data, but because the Stream class is marked as abstract, you most likely need to use one of the several classes derived from Stream. Each Stream derivation is specialized to make transferring data from a specific source easy.
Here is Table to lists some of the classes derived from the Stream class.

TABLE :

CLASS

DESCRIPTION

System.IO.FileStream

Reads and writes files on a file system, as well as other file related operating
system handles (including pipes, standard input, standard output, and so on).

System.IO.MemoryStream

Creates streams that have memory as a backing store instead of a disk or a network
connection. This can be useful in eliminating the need to write temporary files
to disk or to store binary information in a database.

System.IO.UnmanagedMemoryStream

Supports access to unmanaged memory using the existing stream-based model and does
not require that the contents in the unmanaged memory be copied to the heap.

System.IO.BufferedStream

Extends the Stream class by adding a buffering layer to read and write operations
on another stream. The stream performs reads and writes in blocks (4096 bytes by
default), which can result in improved efficiency.

System.Net.Sockets.NetworkStream

Implements the standard .NET Framework stream to send and receive data through network
sockets. It supports both synchronous and asynchronous access to the network data
stream.

System.Security.Cryptography.CryptoStream

Enables you to read and write data through cryptographic transformations.

System.IO.Compression.GZipStream

Enables you to compress data using the GZip data format.

System.IO.Compression.DeflateStream

Enables you to compress data using the Deflate algorithm.

System.Net.Security.NegotiateStream

Uses the Negotiate security protocol to authenticate the client,and optionally the
server, in client-server communication.

System.Net.Security.SslStream

Necessary for client-server communication that uses the Secure Socket Layer (SSL)
security protocol to authenticate the server and optionally the client.

In this example creating a byte array the length of the stream, using the Length property to properly size the array, and then passing it to the Read method.
The Read method fills the byte array with the stream data, in this case reading the entire stream into the byte array. If you want to read only a chunk of the stream or to start at a specific point in the stream simply change the value of the parameters you pass to the Read method.

Streams must always be explicitly closed in order to release the resources they are using, which in this case is the file. Failing to explicitly close the stream can cause memory leaks, and it may also deny other users and applications access to the resource.

Saturday, 21 April 2012

There is a situations where you want to modify the ACL lists.
In this example, you give a specific user explicit Full Control rights over the file.
You can use either an existing user or create a new test User account .

The example starts with getting the collection of existing security settings for the file.
After that , using the AddAccessRule method, a new FileSystemAccessRule is created and added to the files collection of security settings.
Creating a new FileSystemAccessRule requires you to provide three constructor parameters, the user you want to assign this rule to (provided in DOMAIN\USERNAME format), the rights
you want to assign to this rule, and the AccessControlType you want to give this rule.
You can specify multiple rights to assign to the rule by using a bitwise Or operator, as shown in the following Example:

Friday, 20 April 2012

There are situations in which you need to check that string is null or Empty or only space in string exist or not.
Previously we have string.IsNullOrEmpty Method. This method only check for Null and Empty, it does not check which spaces..Net 4.0 Introduce new IsNullOrWhiteSpace method in string class.

Thursday, 19 April 2012

There is a need to get the Access Control Lists or ACLs on directories and files.
ACLs are the way resources such as directories and files are secured in the NTFS file system,which is the file system used by most recent versions of Windows.
Manually you can view a file’s ACLs by selecting the Security tab from the file’s Properties dialog.Here is screen shot for this.

(To view original image , click on image)

There is System.AccessControl namespace in the .NET Framework, you can query the file system for the ACL information.

Tuesday, 17 April 2012

When an ASP.NET page is executed, the thread used to execute the code that generates the page by default has a current working directory of that page. It uses this directory as its base directory . If you pass a relative filename into any System.IO class, the file is assumed to be located relative to the current working directory.

The default working directory for the ASP.NET Development Server is a directory under your Visual Studio install root. If you installed Visual Studio in C:\Program Files. Your ASP.NET Development Server working directory would be C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0.

To get the location of working directory using GetCurrentDirectory() method. This method is exist in System.IO.Directory class.
To set the location of working direcotry using SetCurrentDirectory() method. This method is exist in System.IO.Directory class.

Monday, 16 April 2012

Sometimes we want list of files and its details like file name , size , last access time etc... of a directory programmatically.
We can achieve this using DirectoryInfo and FileInfo Class. These classes are available in available in System.IO Namespace.
There is one method GetFiles() of DirectoryInfo class to get array of FileInfo class of particular directory.
This GetFiles() method accepts various parameters like , Search Pattern and Search Option.
Search Pattern means to get file list particular of give search pattern like you can pass "*.txt" it will get only ".txt" files.
Or like "System*" , it will get file name whose name start with System.

Here are sample example to list out all file and it's details of particular directory

Friday, 13 April 2012

Sometimes we have situation in which we want delete all records of table and that table containts identity column as primary key and we want again insert some new records in this table ,
so in this case identity column does not start with 1 but it start with last number that we had before deleted records.
To avoid this situation we need to reset identity column.
After reset identity column it will start with 1.

Here are sample example for that :

We have one table "product_master" , in this table we have one identity column "product_id".
Now we inserted 3 records into this , so the table entries look like this :

After Delete all records using this query :

delete from product_master

Now table is empty.

After delete now if we insert new record. The newly added record have product_id value 4. because identity column does not reset.You can see this in below image

To reset Identity column Here are Syntax:Syntax :

DBCC CHECKIDENT('TableName', RESEED, 0)

Now again delete all records and execute this command :

DBCC CHECKIDENT('product_master', RESEED, 0)

This command resent the identity of given table.

Now again you insert one record this record has product_id value 1. You can see this in below image.

You can get Drive info using DriveInfo class.
DriveInfo class available in System.IO Namespace.
You can get details like name, type, size, and status etc... of each drive.
There is one static method GetDrives() of DriveInfo Class.Syntax :