Monday, 5 March 2018

027 C# File Handleing Part 3

Directoryinfo : Directoryinfo class comes under the namespace System.IO. Directory info class cannot be used directly as there is no static method defined in it . To use Directoryinfo class, you need to create an instance of the directory info. The instance method help to manipulate directory like create,delete, existence check ect.Here are some commonly used methods.
  • Attributes :  Returns attribute of a directory. 
  • CreationTime : Return file/ directory creation time. 
  • Exists : Return Boolean indicating that directory/ file physically exist or not. 
  • FullName : Return file /directory full path. 
  • LastAccessTime : Return file /directory last access time. 
  • Name : Return instance of the name of file /directory. 
  • Create : Used for creation a directory. 
  • Delete : Delete empty directory info. 
  • GetFiles : Return collection of filename of current directory. 
using System;
using System.IO;
namespace student
{
        class student
       {
            static void Main(string[] args)
            {
                 DirectoryInfo fI = new DirectoryInfo("c:\\drirectory1");
 
          //Create
                fI.Create();
               Console.WriteLine("test.txt file created");

              //Attributes
              FileAttributes att = fI.Attributes;
              Console.WriteLine(fI.Attributes);
             //Output:-1

 
           //Exists
            if (fI.Exists)
            {
                Console.WriteLine(fI.Exists);
            }
           //Output:True or False
 
       //Delete
          fI.Delete();
          Console.WriteLine("test.txt file Deleted");

         //CreationTime
         string dt = fI.CreationTime.ToShortDateString();
        Console.WriteLine("Creation time is :"+ dt);


        //FullName
        string fpath = fI.FullName.ToString();
       Console.WriteLine("Full Path is :" + fpath);

       //LastAccessTime
       string dt2 = fI.LastAccessTime.ToShortDateString();
       Console.WriteLine("Last Acess On :" + dt2);
       }
   }
}


Fileinfo : Fileinfo comes under the namespace System.IO. You will not get static method in Fileinfo class , you cannot call them directly. To use Fileinfo class , you need to  create an instance of that class. The instance method help us to manipulate file , such as  create, delete, existence check.Here are some commonly used methods.
  • Attributes : Get or Set current file or directory attribute. 
  • CreationTime  : Return file creation time. 
  • Exists : Return Boolean indicator, indicating the file physically exist or not. 
  • FullName  : Return full path of a file. 
  • LastAccessTime  : Return time when the file was last accessed. 
  • Name : Return name of the file. 
  • Create : Used to create a file : 
  • Delete : Delete a file.

using System;
using System.IO;
namespace student
{
       class student
      {
              public static void main()
              {
                 FileInfo fI = new FileInfo("c:\\drirectory1\\test.txt");

                //Create
                fI.Create();
                Console.WriteLine("test.txt file created");

               //Attributes
               FileAttributes att = fI.Attributes;
              Console.WriteLine(fI.Attributes);
              //Output:-1
 
              //Exists
              if (fI.Exists)
              {
                   Console.WriteLine(fI.Exists);
              }
             //Output:True or False

            //Delete
            fI.Delete();
           Console.WriteLine("test.txt file Deleted");

           //CreationTime
           string dt = fI.CreationTime.ToShortDateString();
          Console.WriteLine("Creation time is :"+ dt);

         //FullName
         string fpath = fI.FullName.ToString();
        Console.WriteLine("Full Path is :" + fpath);

         //LastAccessTime
         string dt2 = fI.LastAccessTime.ToShortDateString();
         Console.WriteLine("Last Acess On :" + dt2);

              }
       }
}


Directory :Directory class comes under the namespace System.IO. Directory class have some static method to manipulate directory. Create, delete, existence check, the file from the directory can be  obtained from this class.Here are some commonly used methods.
  • GetCreationTime : Get creation date and time of a directory. 
  • Exists: Return Boolean indicating that file or directory physically exist or not. 
  • GetLastAccessTime : Return datetime of the specific directory when last accessed. 
  • CreateDirectory : Create a directory. 
  • Delete: Delete a directory. 
  • GetFiles: Return the collection of the file object of the current directory.

using System;
using System.IO;
namespace student
{
          class student
         {
               public static void Main()
             {

                 //CreateDirectory
                  Directory.CreateDirectory("c:\\drirectory1");
                  Console.WriteLine("drirectory1 created.");

               //Exists
              if (Directory.Exists("c:\\drirectory1"))
             {
               Console.WriteLine("drirectory1 exists.");
             }
 
             //Delete
             Directory.Delete("c:\\drirectory1");
            Console.WriteLine("drirectory1 deleted.");

            //GetLastAccessTime
           //Directory.GetLastAccessTime("c:\\drirectory1");
          Console.WriteLine(Directory.GetLastAccessTime("c:\\drirectory1").ToString());

        //GetCreationTime
         Directory.GetCreationTime("c:\\drirectory1");
         Console.WriteLine(Directory.GetCreationTime("c:\\drirectory1").ToString());
       
    //GetFiles
     string[] arr=Directory.GetFiles("c:\\drirectory1");
      foreach (string name in arr)
     {
          Console.WriteLine(name);
     }
    }
}


}



027 C# File Handleing Part 2

MemoryStream : A sequence of array off byte  to read and write operation of file. The syntax is Memory stream(byte[] buffer). The arrayed buffer  we flow from  source to target file sequentially . This technique is useful for GUI  programming, buffer is retain  till it is needed.


Example 
using System;
using System.IO;
class Program
  {
    static void Main()
     {
          string file_path = "C:\\ab\\test.txt";
         string new_file_path = "C:\\ab\\test2.txt";
          
      //Example Read file in Memory Stream
         using (FileStream fs = File.OpenRead(file_path))
          {
               MemoryStream ms = new MemoryStream();
               ms.SetLength(fs.Length);
              fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
        }

       //Example Write file in Using Memory Stream
       using (FileStream fs = File.OpenRead(file_path))
       {
               MemoryStream ms = new MemoryStream();
               ms.SetLength(fs.Length);
              fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
              FileStream new_fs = new FileStream(new_file_path, FileMode.Create, FileAccess.Write);
              ms.WriteTo(new_fs);
             new_fs.Close();
       }
     Console.ReadKey();
    }
}


Reading from and Writing to Text Files     To read text file .Net Framework StreamReader and StreamWriter class is used. Both StreamReader and StreamWriter class comes under the System.IO namespace.

StreamReader Class :It is a class which read character from a byte stream. It is encoded with a particular encoding system. Generally UTF- 8  encoding is used for encoding, it is a default encoding also. StreamReader have several method to read stream of character from a standard text file. Bellow is the example of some methods.


  •   StreamReader.Read : Read the next character from input stream and advanced by one  character.
  •   StreamReader.ReadToEnd : Read all from start to end.
  •   StreamReader.ReadLine : Read entire line from a input Stream.
  •   StreamReader.ReadBlock : Read Specific nummber of character from input stream and buffered.
  •   StreamReader.EndOfStream : Indicator that the Stream has ended or not.
  •   StreamReader.Close : Close StreamReader and release all resource.
  •   StreamReader.Dispose : Releaes all resource.

 Example 

using System; 

using System.IO;

class Program
{
          static void Main()
          {
                string file_path = "C:\\ab\\test.txt"; 

               //Read by Characted by Character
               using (StreamReader reader = new StreamReader(file_path))
                {
                      while(reader.Read()>0)
                     {
                     }
               }
 
        //Read by Line by Line
            using (StreamReader reader = new StreamReader(file_path))
           {
                 while (reader.ReadLine()!= null)
                  {
                  }
          }

         //Read by ReadBlock
        using (StreamReader reader = new StreamReader(file_path))
        {
           char[] ch = new char[3];
          reader.ReadBlock(ch, 0, 3);
       }

        //Read till end of File
       StreamReader reader1 = new StreamReader(file_path);
         while (!(reader1 as StreamReader).EndOfStream)
         {
        }
        Console.ReadKey();
     }
}

StreamWriter  Class : StreamWriter write character to a stream with a particular encoding. The default encoding UTF-8. StreamWriter have several method to write character in a stream. Below is the example of some method.

StreamWriter.Write : White character from Stream.

StreamWriter.WriteLine : Write a line.

StreamWriter.Flush : Clear buffer.

StreamWriter.NewLine :Write a line terminator character.

StreamWriter.Close : Close StreamReader and release all resource.

StreamWriter.Dispose : Releaes all resource.
Example 

using System;

using System.IO;

class Program
{
        static void Main()
        {
            string file_path = "C:\\ab\\test.txt";

           //StreamWriter Write Example
            char[] arr = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o' };

            using (StreamWriter sw = File.CreateText(file_path))
           {
                 for (int i = 1; i <= 10; i++)
                 {
                   sw.Write(arr[i]);
                }
          }
//StreamWriter WriteLine Example
           using (StreamWriter sw = File.CreateText(file_path))
            {
              for (int i = 1; i <= 10; i++)
              {
                 sw.WriteLine("Hellow World 1");
               }
         }

         Console.ReadKey();
     }
}
Reading from and Writing into Binary files


For the purpose of writing and reading binary file binary reader and binary writer classes are used. Binary reader the primitive data from binary files form a stream. A binary file may be thousands of integer value simple data type. BinaryReader also used to read with specific in coding system. 
BinaryWriter classes is for write primitive data as binary files with a specific in coding system.
 

Example : Example of BinaryReader
using System;
using System.IO;
    class Program
    {
        static void Main()
        {
               string file_path = "C:\\ab\\test.dll";
               FileStream fs = File.OpenRead(file_path);
              BinaryReader b = new BinaryReader(fs);
             string msg = b.ReadString();
             b.Close();
            Console.ReadKey();
      }
}

Example : Example of BinaryWriter

using System;
using System.IO;
   class Program
   {
      static void Main()
     {
        string file_path = "C:\\ab\\test.dll";
        using (BinaryWriter writer = new BinaryWriter(File.Open(file_path, FileMode.Create)))
       {
          writer.Write("Error Writing File");
       }
       Console.ReadKey();
   }
}





বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...