Export (0) Print
Expand All

FileInfo Constructor

Initializes a new instance of the FileInfo class, which acts as a wrapper for a file path.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

Public Sub New ( _
	fileName As String _


Type: System.String

The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.


fileName is Nothing.


The caller does not have the required permission.


The file name is empty, contains only white spaces, or contains invalid characters.


Access to fileName is denied.


The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.


fileName contains a colon (:) in the middle of the string.

You can specify either the fully qualified or the relative file name, but the security check gets the fully qualified name.

The following example uses this constructor to create two files, which are then written to, read from, copied, and deleted.

Imports System
Imports System.IO

Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt" 
        Dim fi1 As FileInfo = New FileInfo(path)

        If fi1.Exists = False Then 
            'Create a file to write to. 
            Dim sw As StreamWriter = fi1.CreateText()
        End If 

        'Open the file to read from. 
        Dim sr As StreamReader = fi1.OpenText()

        Do While sr.Peek() >= 0

            Dim path2 As String = path + "temp" 
            Dim fi2 As FileInfo = New FileInfo(path2)

            'Ensure that the target does not exist.

            'Copy the file.
            Console.WriteLine("{0} was copied to {1}.", path, path2)

            'Delete the newly created file.
            Console.WriteLine("{0} was successfully deleted.", path2)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try 
    End Sub 
End Class 
'This code produces output similar to the following;  
'results may vary based on the computer/file structure/etc.: 

'c:\MyTest.txt was copied to c:\MyTest.txttemp. 
'c:\MyTest.txttemp was successfully deleted.

The following example opens an existing file or creates a file, appends text to the file, and displays the results.

Imports System
Imports System.IO

Public Class FileInfoMainTest

    Public Shared Sub Main()
        ' Open an existing file, or create a new one. 
        Dim fi As New FileInfo("temp.txt")
        ' Create a writer, ready to add entries to the file. 
        Dim sw As StreamWriter = fi.AppendText()
        sw.WriteLine("This is a new entry to add to the file")
        sw.WriteLine("This is yet another line to add...")
        Dim sr As New StreamReader(fi.OpenRead())
        ' Get the information out of the file and display it. 
        While sr.Peek() <> -1
        End While 
    End Sub 'Main
End Class 'FileInfoMainTest
'This code produces output similar to the following;  
'results may vary based on the computer/file structure/etc.: 

'This is a new entry to add to the file 
'This is yet another line to add...

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft