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);
     }
    }
}


}



1 comment:

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

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