Freigeben über


FileMode Enumeration

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

Specifies how the operating system should open a file.

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Enumeration FileMode
[ComVisibleAttribute(true)]
public enum FileMode

Members

Member name Description
Supported by Silverlight for Windows PhoneSupported by Xbox 360 CreateNew Specifies that the operating system should create a new file.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Create Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Open Specifies that the operating system should open an existing file. The ability to open the file is dependent on the value specified by FileAccess. A System.IO.FileNotFoundException is thrown if the file does not exist.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 OpenOrCreate Specifies that the operating system should open a file if it exists; otherwise, a new file should be created.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Truncate Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Append Opens the file if it exists and seeks to the end of the file, or creates a new file. Append can only be used in conjunction with Write. Attempting to seek to a position before the end of the file will throw an IOException and any attempt to read fails and throws an NotSupportedException.

Remarks

Specify a FileMode parameter IsolatedStorageFileStream constructors.

Examples

The following example opens an existing file. This example is part of a larger example provided for the IsolatedStorageFile class.

' Write to an existing file: MyApp1\SubDir1\MyApp1A.txt

' Determine if the file exists before writing to it.
Dim filePath As String = Path.Combine(subdirectory1, "MyApp1A.txt")

If store.FileExists(filePath) Then
    Try
        Using sw As StreamWriter = _
            New StreamWriter(store.OpenFile(filePath, FileMode.Open, FileAccess.Write))

            sw.WriteLine("To do list:")
            sw.WriteLine("1. Buy supplies.")
        End Using

    Catch ex As IsolatedStorageException
        sb.AppendLine(ex.Message)
    End Try
Else
    sb.AppendLine((filePath + "does not exist"))
End If
// Write to an existing file: MyApp1\SubDir1\MyApp1A.txt

// Determine if the file exists before writing to it.
string filePath = Path.Combine(subdirectory1, "MyApp1A.txt");

if (store.FileExists(filePath))
{
    try
    {
        using (StreamWriter sw =
            new StreamWriter(store.OpenFile(filePath,
                FileMode.Open, FileAccess.Write)))
        {
            sw.WriteLine("To do list:");
            sw.WriteLine("1. Buy supplies.");
        }
    }
    catch (IsolatedStorageException ex)
    {

        sb.AppendLine(ex.Message);
    }
}

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.

See Also

Reference

Other Resources