Aracılığıyla paylaş


File Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.

Inheritance Hierarchy

System.Object
  System.IO.File

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class File
[ComVisibleAttribute(true)]
public static class File

The File type exposes the following members.

Methods

  Name Description
Public methodStatic member AppendAllLines(String, IEnumerable<String>) When it is called by trusted applications, appends lines to a file, and then closes the file.
Public methodStatic member AppendAllLines(String, IEnumerable<String>, Encoding) When it is called by trusted applications, appends lines to a file by using a specified encoding, and then closes the file.
Public methodStatic member AppendAllText(String, String) When it is called by trusted applications, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
Public methodStatic member AppendAllText(String, String, Encoding) When called by trusted applications, appends the specified string to the file, creating the file if it does not already exist.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 AppendText When it is called by trusted applications, creates a StreamWriter that appends UTF-8 encoded text to an existing file.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Copy(String, String) When it is called by trusted applications, copies an existing file to a new file. Overwriting a file of the same name is not allowed.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Copy(String, String, Boolean) When it is called by trusted applications, copies an existing file to a new file. Overwriting a file of the same name is allowed.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Create(String) When it is called by trusted applications, creates or overwrites a file in the specified path.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Create(String, Int32) When it is called by trusted applications, creates or overwrites the specified file.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 CreateText When it is called by trusted applications, creates or opens a file for writing UTF-8 encoded text.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Delete When it is called by trusted applications, deletes the specified file. An exception is not thrown if the specified file does not exist.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Exists When it is called by trusted applications, determines whether the specified file exists.
Public methodStatic member GetAttributes Gets the FileAttributes of the file on the path.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetCreationTime When it is called by trusted applications, returns the creation date and time of the specified file or directory.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetLastAccessTime When it is called by trusted applications, returns the date and time the specified file or directory was last accessed.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetLastWriteTime When it is called by trusted applications, returns the date and time the specified file or directory was last written to.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Move When it is called by trusted applications, moves a specified file to a new location, providing the option to specify a new file name.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Open(String, FileMode) When it is called by trusted applications, opens a FileStream on the specified path with read/write access.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Open(String, FileMode, FileAccess) When it is called by trusted applications, opens a FileStream on the specified path, with the specified mode and access.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Open(String, FileMode, FileAccess, FileShare) When it is called by trusted applications, opens a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 OpenRead When it is called by trusted applications, opens an existing file for reading.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 OpenText When it is called by trusted applications, opens an existing UTF-8 encoded text file for reading.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 OpenWrite When it is called by trusted applications, opens an existing file for writing.
Public methodStatic member ReadAllBytes When it is called by trusted applications, opens a binary file, reads the contents of the file into a byte array, and then closes the file.
Public methodStatic member ReadAllText(String) When it is called by trusted applications, opens a text file, reads all lines of the file, and then closes the file.
Public methodStatic member ReadAllText(String, Encoding) When it is called by trusted applications, opens a file, reads all lines of the file with the specified encoding, and then closes the file.
Public methodStatic member ReadLines(String) When it is called by trusted applications, reads the lines of a file.
Public methodStatic member ReadLines(String, Encoding) When it is called by trusted applications, read the lines of a file that has a specified encoding.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 SetAttributes Security Critical. Sets the specified FileAttributes of the file on the specified path.
Public methodStatic member WriteAllBytes When it is called by trusted applications, creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
Public methodStatic member WriteAllLines(String, IEnumerable<String>) When it is called by trusted applications, creates a new file, writes a collection of strings to the file, and then closes the file.
Public methodStatic member WriteAllLines(String, IEnumerable<String>, Encoding) When it is called by trusted applications, creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
Public methodStatic member WriteAllText(String, String) When it is called by trusted applications, creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
Public methodStatic member WriteAllText(String, String, Encoding) When it is called by trusted applications, creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten.

Top

Remarks

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

This type is present to support the .NET Compact Framework infrastructure in Silverlight for Windows Phone, and it is not intended to be used in your application code.

Examples

The following example uses a File in a trusted application to determine if a file exists in a users' My Documents folder. This code example is part of a larger example provided for the StreamReader class.

Private Sub OpenFile_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

    If Application.Current.HasElevatedPermissions Then
    
        ' fileLoc is a global string variable.
        fileLoc = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyDoc.txt")

        ' Create the file if it does not exist.
        If Not File.Exists(fileLoc) Then
            Using swNew As New StreamWriter(fileLoc)
                swNew.WriteLine("Sample text")
            End Using
        End If

        ' Display the contents in a TextBox.
        Using sr As New StreamReader(fileLoc)
            inputData.Text = sr.ReadToEnd()
            inputData.Visibility = Visibility.Visible
        End Using
    End If
End Sub
private void OpenFile_Click(object sender, RoutedEventArgs e)
{

    if (Application.Current.HasElevatedPermissions)
    {
        // fileLoc is a global string variable.
        fileLoc = System.IO.Path.Combine(Environment.GetFolderPath(
           Environment.SpecialFolder.MyDocuments), "MyDoc.txt");

        // Create the file if it does not exist.
        if (!File.Exists(fileLoc))
        {
            using (StreamWriter swNew = new StreamWriter(fileLoc))
            {
                swNew.WriteLine("Sample text");
            }
        }

        // Display the contents in a TextBox.
        using (StreamReader sr = new StreamReader(fileLoc))
        {
            inputData.Text = sr.ReadToEnd();
            inputData.Visibility = Visibility.Visible;
        }
    }
}

Version Information

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

Platforms

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

Thread Safety

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

See Also

Reference