Export (0) Print
Expand All

IsolatedStorageFile.CreateDirectory Method

Creates a directory in the isolated storage scope.

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

public void CreateDirectory(
	string dir
)

Parameters

dir
Type: System.String

The relative path of the directory to create within the isolated storage scope.

ExceptionCondition
IsolatedStorageException

The current code has insufficient permissions to create isolated storage directory.

ArgumentNullException

The directory path is null.

The created directory initially contains no files. If the directory already exists, the CreateDirectory method returns without creating a new directory. The How to: Create Files and Directories in Isolated Storage example demonstrates the use of the CreateDirectory method

The following code example demonstrates the CreateDirectory method. For the complete context of this example, see the IsolatedStorageFile overview.

public double SetNewPrefsForUser()
{
    try
    {
        byte inputChar;
        IsolatedStorageFile isoFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly |
            IsolatedStorageScope.Domain,
            typeof(System.Security.Policy.Url),
            typeof(System.Security.Policy.Url));

        // If this is not a new user, archive the old preferences and  
        // overwrite them using the new preferences. 
        if (!this.myNewPrefs)
        {
            if (isoFile.GetDirectoryNames("Archive").Length == 0)
                isoFile.CreateDirectory("Archive");
            else
            {

                IsolatedStorageFileStream source =
                    new IsolatedStorageFileStream(this.userName, FileMode.OpenOrCreate,
                    isoFile);
                // This is the stream from which data will be read.
                Console.WriteLine("Is the source file readable? " + (source.CanRead ? "true" : "false"));
                Console.WriteLine("Creating new IsolatedStorageFileStream for Archive.");

                // Open or create a writable file.
                IsolatedStorageFileStream target =
                    new IsolatedStorageFileStream("Archive\\ " + this.userName,
                    FileMode.OpenOrCreate,
                    FileAccess.Write,
                    FileShare.Write,
                    isoFile);
                Console.WriteLine("Is the target file writable? " + (target.CanWrite ? "true" : "false"));
                // Stream the old file to a new file in the Archive directory. 
                if (source.IsAsync && target.IsAsync)
                {
                    // IsolatedStorageFileStreams cannot be asynchronous.  However, you 
                    // can use the asynchronous BeginRead and BeginWrite functions 
                    // with some possible performance penalty.

                    Console.WriteLine("IsolatedStorageFileStreams cannot be asynchronous.");
                }

                else
                {
                    Console.WriteLine("Writing data to the new file.");
                    while (source.Position < source.Length)
                    {
                        inputChar = (byte)source.ReadByte();
                        target.WriteByte(inputChar);
                    }

                    // Determine the size of the IsolatedStorageFileStream 
                    // by checking its Length property.
                    Console.WriteLine("Total Bytes Read: " + source.Length);

                }

                // After you have read and written to the streams, close them.
                target.Close();
                source.Close();
            }
        }

        // Open or create a writable file with a maximum size of 10K.
        IsolatedStorageFileStream isoStream =
            new IsolatedStorageFileStream(this.userName,
            FileMode.OpenOrCreate,
            FileAccess.Write,
            FileShare.Write,
            10240,
            isoFile);
        isoStream.Position = 0;  // Position to overwrite the old data.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft