FileInfo class in VB.NET

This article describes FileInfo class in VB.NET.
  • 8028

This article describes FileInfo class in VB.NET. System.IO provides all the necessary classes, methods, and properties for manipulating directories and files.

FileInfo class

When we perform multiple operations on the same file, use FileInfo. FileInfo class is used to create new files, access information about the files, delete, and move files. This class also provides methods for opening, reading from, and writing to a file.

Methods

FileInfo class contains following method.

CopyTo(String) - This method is used to Copies an existing file to a new file, disallowing the overwriting of an existing file.
Create method - This method is used to create a file.
Delete method - This method is used to delete Permanently a file.
Equals(Object)  - Equals Determines whether the specified Object is equal to the current Object.
Open(FileMode) - This method opens a file in the specified mode.

Properties

The FileInfo class provides the following properties that enable you to retrieve information about a file.
Directory property - The Directory property retrieves an object that represents the parent directory of a file.
DirectoryName property - The DirectoryName property retrieves the full path of the parent directory of a file.
Exists property - The Exists property checks for the presence of a file before operating on it.
IsReadOnly property - The IsReadOnly property retrieves or sets a value that specifies whether a file can be modified.
Length property - The Length retrieves the size of a file.
Name property - The Name retrieves the name of a file.

For example

In this example we can easily write texts or other information to a file by using the CreateText() method of the FileInfo class.

VB code

Imports System.IO

Module Module1

    Sub Main()

        Dim f As New FileInfo("d:\Mytext.txt")

        Dim w As StreamWriter = f.CreateText()

        w.WriteLine("This is Rohatash kumar")

        w.WriteLine("Email- [email protected]")

        w.WriteLine("address- mathura")

        w.Write(w.NewLine)

        w.WriteLine("Qualification - master degree")

        w.Close()

    End Sub

End Module

C# code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

     FileInfo f = new FileInfo(@"d:\Mytext.txt");

     StreamWriter w = f.CreateText();

     w.WriteLine("This is Rohatash kumar");

     w.WriteLine("Email- [email protected]");

     w.WriteLine("address- mathura");

     w.Write(w.NewLine);

     w.WriteLine("Qualification - master degree");

     w.Close();

        }

    }

}

This create a text file mytext in D drive and write all the text in this file.  

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.