Breaking News

Sunday 29 September 2013

File I/O Using Windows File System C#....

C# - Windows File System:

C# allows you to work with directories and files using various directory and file related classes like , the DirectoryInfo class and the FileInfo class.

The DirectoryInfo Class:

The DirectoryInfo class is derived from the FileSystemInfo class. it have various method for creating , moving , and browsing , through directories and subdirectories. this class can not be inherited.

some commonly used properties of the DirectoryInfo class:



  1. Attributes  >> Gets the attributes for the current file or directory.
  2. CreationTime >> Gets the creation time of current file or directory.
  3. Exists >> gets a Boolean value indicating whether the directory exists.
  4. Extension >> Gets the string representing the file extension.
  5. FullName >> Gets the full path of the directory or file.
  6. LastAccessTime >> Gets the time the current file or directory was last accessed.
  7. Name >> Gets the name of this DirectoryInfo instance.

Some commonly used methods of the DirectoryInfo class:

  1. Create()  >> Creates a directory.
  2. CreateSubDirectory(string) >> Create a subdirectory  and subdirectories on the specified path can be relative to this instance of the directory class.
  3. Delete() >> Delete directory info if its empty.
  4. GetDirectories() >> return the subdirectories of the current directories.
  5. GetFiles() >> return a file list from the current directory.
 
The FIleInfo Class:

The FileInfo Class is derived from the FileSystemInfo class. it has properties and instance method  for creating , copying , moving , and opening of files , and helps in the creation of FileStream object. this class cannot be inherit.

some commonly used properties of the FileInfo class:


  • Attributes  >> Gets the attributes for the current file.
  • CreationTime >> Gets the creation time of current file.
  • Exists >> gets a Boolean value indicating whether the file exists.
  • Extension >> Gets the string representing the file extension.
  • FullName >> Gets the full path of the file.
  • LastAccessTime >> Gets the time the current file was last accessed.
  • Name >> Gets the name of this file instance.
  • Directory >> Gets an instance of an directory which the file belongs.
  • LastWriteTime >> gets the time of the last written activity of the file.
  • length >> Gets the size , in bytes , of the current files.
  • Name >> Gets the name of the file.

  • some commonly used method of FileInfo class:

    1. AppendText()  >> Create a StreamWriter that appends text to the file represented by this instance of the FileInfo.
    2. Create()  >> create a file.
    3. Delete()  >> deletes a file permanently.
    4. MoveTo(string destFilename) >> moves a specific file to the new location , providing the option to specify a new file name.
    5. Open(FileMode mode, FileAccess access, FileShare share)  >> opens a file in specified mode with read , write or read/write access and the specified sharing option.
    6. OpenRead()  >> creates a read only FileStream.
    7. OpenWrite()  >> creates a write only FileStream.

    the following example demonstrate the use of DirectoryInfo class :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace directoryinfo_class.cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                DirectoryInfo mydirinfo = new DirectoryInfo("C:");
                Console.WriteLine("full name of the directory is :"+ mydirinfo.FullName);
                Console.WriteLine("the directory was last accessed on :"+ mydirinfo.LastAccessTime.ToString());
                Console.ReadKey();
            }
        }
    }

    output:



    the following example demonstrate the use of FileInfo class :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace fileinfo_classes.cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                // creating an instance of directoryinfo class
                DirectoryInfo mydirinfo = new DirectoryInfo("C:");
                // set all the files in the directory and print their name and size
                FileInfo[] fileindir = mydirinfo.GetFiles();
                foreach (FileInfo file in fileindir)
                {
                        Console.WriteLine("file name : " + file.Name + " extension :" + file.Extension + " size : " + file.Length + "bytes");
                }
                Console.ReadKey();
            }
        }
    }


    output :


    reference link :


    Read more ...

    Thursday 26 September 2013

    c# File IO using Reading from and Writing into Binary Files

    The BinaryReader and BinaryWriter classes are used for reading from and writing to s binary file.

    The BinaryReader class:

    The BinaryReader classes is used to read Binary data from a file. A BinaryReader object is created by passing a FileStream Object to its constructor.

    some of the commonly used method of BinaryReader class :


    • Close()  >> it closes the BinaryReader object and the underlying stream.
    • Read()  >> read the character from the underlying stream.
    • ReadBoolean()  >> reads a boolean value from current stream and advances current the position of the stream.
    • ReadByte()  >> reads the next bytes from the current stream.
    • ReadChar() >> reads the next character from the current stream.
    • ReadDouble() >> reads an floating points value from the current stream.
    • ReadString() >> reads a string from a current stream.
    The BinaryWriter Class:

    The BinaryWriter class is used to Write data to a stream. A BinaryWriter object is created by passing a FileStream object to its constructor.

    some of the commonly used method of BinaryWriter class :

    • Close()  >> its close the BinaryWriter class object and underlying stream.
    • Flush()   >> clear all buffer for the current writer.
    • Seek(int offset , SeekOrigin Origin)  >> sets the position within the current stream.
    •  Write(bool value)  >> write a one-byte boolean value to the current stream.
    • Write(byte value)  >> write an unsigned byte to the current stream.
    • Write(byte[] buffer)  >> writes a byte array to the underlying stream.
    • Write(string value)  >> reads a string value from the current stream.

    Example:


    using System.IO;

    namespace binaryreader_and_binarywriter_classes.cs
    {
        public class RWDATA
        {
            static void Main(string[] args)
            {
                int i = 10;
                double d = 1023.23;
                bool b = true;

                FileStream fs = new FileStream("C://USING IO/testdata",FileMode.Create);
                BinaryWriter dataout = new BinaryWriter(fs);
                Console.WriteLine("writing"+i);
                dataout.Write(i);
                Console.WriteLine("writing"+b);
                dataout.Write(b);
                Console.WriteLine("writing"+d);
                dataout.Write(1023.23);
                dataout.Close();
                Console.WriteLine("");
                // now read them back
                FileStream fs1 = new FileStream("C://USING IO/testdata", FileMode.Open);
                BinaryReader datain = new BinaryReader(fs1);
                i = datain.ReadInt32();
                Console.WriteLine("reading "+ i);
                b = datain.ReadBoolean();
                Console.WriteLine("reading "+ b);
                d = datain.ReadDouble();
                Console.WriteLine("reading "+1023.23);
                Console.ReadLine();
                datain.Close();
            }
        }
    }


    output :




    Reference Link :




    Read more ...

    C# File I/O using Read and Write into Text File

    C# READING AND WRITING TO TEXT FILES :

    the StreamReader and StreamWriter Classes are used for reading from and writing data to text files. these classes inherit from the base class stream , which supports reading and writing bytes into a file stream.

    The StreamReader Class :

    the StreamReader class also inherit from the abstract base class TextReader that represents a reader for reading series of characters.

    some of the commonly used method of the StreamReader Class :


    • Close()  >> it closes the StreamReader object and Stream , and release any system resource associated with the reader.
    • Peek()   >> return the next available character but does not consume it.
    • Read()  >> reads the next character from the input stream.
    example :

    the following example demonstrates reading a text file named test.txt. the file reads :

    using system;
    using system.IO;

    namespace fileapplication
    {
       class program
         {
            static void Main( String[] args )
              {
                 try
                  {
                     // create an instance of StreamReader to read from a file.
                    // the using statement also closed the StreamReader.
                      using( StreamReader sr = new StreamReader ( "C://test.txt"))
                        {
                          string line;
                            // read and display lines from the files until
                            // the end of the file is reached.
                            While (( line = sr.ReadLine()) != null)
                               {
                                  Console.WriteLine(line);
                               }
                          }
                      }
        catch ( Exception e )
          {
             // let the user know what went wrong.
             Console.WriteLine(" the file could not be read.."+ e.Message);
          }
    Console.ReadKey();
           }
        }
     }

     guess what display when you compile and run the program..

    example 2..


    using System.IO;

    namespace srreamreader_class.cs

    {

        class Program

        {
            public void readData()
            {
                FileStream fs = new FileStream("C://myfile/myfile.txt", FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                // position the file pointer at the beginning of the file..
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                // read till the end of the file
                string str = sr.ReadLine();
                while (str != null)
                {
                    Console.WriteLine("" + str);
                    str = sr.ReadLine();
                }
                sr.Close();
                fs.Close();
                Console.ReadKey();
            }
            static void Main(string[] args)
            {
                Program p = new Program();
                p.readData();
            }
        }
    }


    output :



    reference link :


    The StreamWriter Class :

    the StreamWriter class inherits from the abstract from the abstract classes TextWriter represent a writer , which can write a series of a character.

    the mos commonly used method of StreamWriter Class are:


    • Close()   >> close the current StreamWriter class and Stream.
    • Flush()  >> clears all buffers for current writer.
    • Write(bool value) >> write the text value to the text string or stream. ( inherited from TextWriter.)
    • Write(char value) >> writes a character to the stream.
    • Write(decimal value) >> write the text representation of decimal value to the text string or stream.
    • Write(double value) >> write the text representation of 8-byte floating point  value to the text string or stream.
    • Write( int value) >> write the text representation of 4-byte integer value to the text string or stream.
    • Write( string value) >> writes a string to the stream.
    • WriteLine() >> write a line to the stream.

    the following code demonstrates writing text data into a file using the StreamWriter Class:


    using System.IO;
    namespace streamwriter_class.cs
    {
        class Program
        {
            public void writedata()
            {
                FileStream fs = new FileStream("C://myfile/myfile.txt", FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                // enter a string which is stored in the file myfile.txt
                Console.WriteLine("enter a string..");
                string str = Console.ReadLine();
                //write the string entered by the user in the file myfile.txt
                sw.Write(str);
                // clear all buffer of the current writer
                sw.Flush();
                sw.Close();
                fs.Close();
                Console.ReadKey();
            }
            static void Main(string[] args)
            {
               
                Program p = new Program();
                p.writedata();
            }
        }

    }

    output :




    reference link:




    click here to watch an example of StreamWriter Class .
    Read more ...

    how to use File IO using StreamReader class C#

    Read more ...

    Wednesday 25 September 2013

    c# FILE I/O ( Input stream and output stream )

    A file is collection of data stored in a disk with specific name and a directory path. when a file is opened for reading or writing , its became a stream.

    Now what is stream?

    the stream is basically the sequence of bytes passing through the communication path.

    there are two main stream : the input stream and the output stream.
    the input stream is used for reading data from a file and the output stream is used for write into the file.

    C# I/O Classes

    The system.IO namespace have various classes that are used for performing various operation with file , like creating and deleting file , reading from and writing into a file , closing a file etc.

    some commonly used non-abstract classes in the system.IO namespace:

    • Binary Reader          >>  reads primitive data from a binary stream.
    • Binary Writer           >>  writes primitive data in binary format.
    • Buffered Stream       >>  a temporary storage for a stream of bytes.
    • Directory                  >>  helps in manipulating a directory structure.
    • Directory Info          >>  used for performing operation on directories.
    • Drive Info                >>  provides information for the drive.
    • File                          >>  helps in manipulating files.
    • File Info                  >>  used for performing operation on files.
    • File Stream             >>  used to read from and write to any location in a file.
    • Memory Stream     >>  used for random access to streamed data stored in memory.
    • path                       >>  performs operation on paths information.
    • Stream Reader       >>  used for reading character from a byte stream.
    • Stream Writer       >>  is used to write character to a stream.
    • String Reader       >>  is used for reading from a string buffer.
    the FileStream class :

    the FileStream class in the system.IO namespace helps in reading from , writing to and closing files. this class derived from the abstract class Stream.

    you need to create a FileStream object  to create a new file or open an existing file. 

    syntax:

    FileStream  <object name> = new FileStream( <file name>, <FileMode Enumerator>, < FileAccess Enumerator>, <FileShare Enumerator>);

    for example :

    FileStream fs = new FileStream ("sample.txt", FileMode.Open, FileAccess. Read, FileShare. Read);

    • FileMode >> the FileMode Enumerator define various method for opening files. the                                 member of the file mode enumerator are :
                                                 Append           >> it open an exist file and puts cursor at the end of                                                                          file,  or create a file if file does not exist.

               Create                 >> it is creates a new file.

       CreateNew          >> create a new file.

              Open                 >> it open an existing file.

                                                   OpenOrCreate  >> open a file if exist , otherwise its should create                          a new file.
     
                                                  Truncate           >> it is open a file and Truncates its size to zero                    bytes.
    • FileAccess >> FileAccess enumerator have member : Read , ReadWrite , Write.
    • FileShare  >> FileShare enumerator have the following members:
                                         Inheritable >> it allow a file handle to pass inheritance to the child process.

                             None >> it declines sharing of the current file.

                           Read >> it allow opening the file for reading.

                                  ReadWrite >>  it allow opening a file for reading and writing.

                        Write >> it allow opening the file for writing.

    example:

    the following program demonstrates use of the FileStream class :

    using system;
    using system.IO;

    namespace FileToApplication
    {
        class program
          {
             static void Main(String [] args)
               {
                  FileStream fs = new FileStream ( "test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);

    for ( int i = 1; i<=20; i++)
       { 
           fs.WriteByte ((byte)i);
        }
    fs.Position = 0;

    for ( int i = 0; i<=20; i++)
        {
           Console.Write( fs.ReadByte() + "");
        }
    fs.Close();
    Console.ReadKey();
             }
        }
    }

    when the above code is compiled and executed , its produces the following result :


    Read more ...

    how to implement File IO using FileStream Class C#

    Read more ...

    understanding exception handling C#

    Read more ...

    Saturday 21 September 2013

    working of arithmetic assignment operator

    Read more ...

    creating a sample C# program

    Read more ...

    how to use StreameReader class in C# IO

    Read more ...

    garbage collection

    Read more ...

    working of unary operator

    Read more ...

    using loopsC#

    Read more ...

    types of access specifires

    Read more ...

    passing values as a parameters C#

    Read more ...

    conditional constructs

    Read more ...

    amazing ice skating skills

    Read more ...

    THE INCREDIBLE INDIA

    Read more ...

    bike stunt comedy

    Read more ...

    AWESOME SHYARI (HINDI)

    Read more ...

    helpful friend :D

    Read more ...

    Sunday 15 September 2013

    amazing ice skating skills

    Read more ...

    shocking

    Read more ...

    comedy video

    Read more ...

    amazing hand art must watch

    Read more ...

    top incident

    Read more ...

    playing with small baby

    Read more ...

    khara smaundra hindi story

    Read more ...

    comedy animation short film hindi

    Read more ...

    Wednesday 11 September 2013

    Friday 6 September 2013

    good morning

    Read more ...

    TOP 10 SLEEPY ANIMAL'S

    Read more ...

    AWESOME PURPOSE-AL

    A MAN PURPOSE A GIRL
            CLICK HERE TO WATCH :>> WATCH HERE


    Read more ...

    Thursday 5 September 2013

    prank with a man

    Read more ...

    cricket comedy

    Read more ...

    comedy add

    Read more ...

    if you think pollution doesn't affect you "think again"

    Read more ...

    comedy 2

    Read more ...

    extraordinary beer shop

    Read more ...

    prank with dad

    Read more ...

    kid comedy with people

    Read more ...

    AGE EFFECT

    Read more ...

    A GOLD SMITH MAKE A GOLD T-SHIRT 3 KG OF GOLD (HINDI)

    Read more ...

    KID DANCE

    Read more ...

    AMAZING VIDEO MUST WATCH

    Read more ...

    Tuesday 3 September 2013

    Sunday 1 September 2013

    scary movie 6

    Read more ...

    comedy 1

    watch here :>> click here
    Read more ...

    gangnam style with paper art

    watch here :>> click here
    Read more ...

    cute girl dancing at home

    watch here :>>click here
    Read more ...

    watch magic show ....

    watch here :>>click here
    Read more ...

    girls fight as cat fight

    Read more ...

    a comedy seeen from a Hindi movie..

    click here to watch :>> click here
    Read more ...

    comedy add for people who do this...:)

    for more :>>  click here
    Read more ...

    owsme dance by a man must watch..:)

    click here for more :>>click here
    Read more ...

    cute kid dance on gangnam style song ....

    watch here :>> click here
    Read more ...

    horrible girl

    click here :>>click here
    Read more ...

    prank with friend by his friend ...

    watch here :>> click here
    Read more ...

    stunt by a man over the car

    watch here ;>>click here
    Read more ...

    sacleton dance must watch

    watch here :>>click here
    Read more ...

    killer boyfriend

    for more click here  :>> click here
    Read more ...

    great dance by the girl with her dog in talent show must watch

    for watch more : >> click here
    Read more ...
    © 2013 Post by repter_x