Export (0) Print
Expand All

FileInfo Class

Provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. This class cannot be inherited.

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

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class FileInfo _
	Inherits FileSystemInfo

The FileInfo type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FileInfoWhen it is called by trusted applications, initializes a new instance of the FileInfo class, which acts as a wrapper for a file path.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360AttributesWhen called by trusted applications, gets or sets the FileAttributes of the current FileSystemInfo. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CreationTimeWhen called by trusted applications, gets or sets the creation time of the current FileSystemInfo object. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360DirectoryWhen it is called by trusted applications, gets an instance of the parent directory.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360DirectoryNameWhen it is called by trusted applications, gets a string representing the directory's full path.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360ExistsGets a value indicating whether a file exists. (Overrides FileSystemInfo.Exists.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360ExtensionGets the string representing the extension part of the file. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360FullNameWhen called by trusted applications, gets the full path of the directory or file. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360LastAccessTimeWhen called by trusted applications, gets or sets the time the current file or directory was last accessed. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360LastWriteTimeWhen called by trusted applications, gets or sets the time when the current file or directory was last written to. (Inherited from FileSystemInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360LengthGets the size, in bytes, of the current file.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NameGets the name of the file. (Overrides FileSystemInfo.Name.)
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360AppendTextWhen it is called by trusted applications, creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CopyTo(String)When it is called by trusted applications, copies an existing file to a new file, disallowing the overwriting of an existing file.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CopyTo(String, Boolean)When it is called by trusted applications, copies an existing file to a new file, allowing the overwriting of an existing file.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CreateWhen it is called by trusted applications, creates a file.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CreateTextWhen it is called by trusted applications, creates a StreamWriter that writes a new text file.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360DeleteWhen it is called by trusted applications, permanently deletes a file. (Overrides FileSystemInfo.Delete.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MoveToWhen it is called by trusted applications, moves a specified file to a new location, providing the option to specify a new file name.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Open(FileMode)When it is called by trusted applications, opens a file in the specified mode.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Open(FileMode, FileAccess)When it is called by trusted applications, opens a file in the specified mode with read, write, or read/write access.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Open(FileMode, FileAccess, FileShare)When it is called by trusted applications, opens a file in the specified mode with read, write, or read/write access and the specified sharing option.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360OpenReadCreates a read-only FileStream.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360OpenTextCreates a StreamReader with UTF8 encoding that reads from an existing text file.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360OpenWriteWhen it is called by trusted applications, creates a write-only FileStream.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360RefreshRefreshes the state of the object. (Inherited from FileSystemInfo.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns the path as a string. (Overrides Object.ToString.)
Top

  NameDescription
Protected fieldSupported by Silverlight for Windows PhoneSupported by Xbox 360FullPathInfrastructure. Represents the fully qualified path of the directory or file. (Inherited from FileSystemInfo.)
Protected fieldSupported by Silverlight for Windows PhoneSupported by Xbox 360OriginalPathInfrastructure. The path originally specified by the user, whether relative or absolute. (Inherited from FileSystemInfo.)
Top

The following example displays an image picked at random from the user's My Pictures folder. It uses the DirectoryInfo class to obtain an enumerable collection of FileInfo objects that represent files that have a .jpg or .png extension. That collection is used to construct a List(Of T) collection so that its index can be used to select a file that corresponds to the random number.

The example then creates a bitmap image by using the FileStream class and sets it as the source for an Image control (named myImage).

For example code and information about how to create an application that runs outside the browser, see Out-of-Browser Support.


Private Sub LoadImage()

	If Application.Current.HasElevatedPermissions Then
		Dim di As New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures))

		Dim files = From f In di.EnumerateFiles() _
			Where f.Extension.ToLower() = ".jpg" OrElse f.Extension = ".png" _
			Select f

		Dim max As Integer = files.Count() + 1

		Dim rnd As New Random()
		Dim r As Integer = rnd.[Next](0, max)

		Dim pics As New List(Of FileInfo)(files)

		Dim randpic As String = pics(r).FullName

		Dim img As New BitmapImage()
		img.SetSource(New FileStream(randpic, FileMode.Open))


		MyImage.Source = img
	End If
End Sub


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft