Use .NET IO (4)

zhaozj2021-02-17  51

Get a storage area

The virtual file system in the storage area is publicly available. IsolatedStorageFile provides a number of methods for interacting with the storage area. To create and retrieve storage, IsolatedStorageFile provides three static methods. Call GetUsersToreFoSsembly or getUsersToreFordomain returns to the storage of by users and assessments and by user, domain, and program set. These two methods retrieve the memory area belonging to the code block (which is called from the code block). The static method GetStore returns a stand-alone storage area, which is specified by a combination of incoming range parameters. The following parameters returns a storage area isolated by the user, assembly, and domain.

[C #]

GetStore (isolatedStorageScope.user | isolatedStorageScope.assembly | IsolatedStorageScope.domain, null, null;

The GetStore method can be used to specify that the storage area should be roamed with the roaming user profile.

By default, the independent storage area obtained from different assemblies is different. You can access different assemblies or domains, which are incoming different assemblies or domain evidences as the last two parameters of the getStore method. This requires access to the independent storage of the application domain identifies. See the GetStore method for more information. For more information on the assembly, see the assembly.

Each method in the three methods is returned to the ISOLATEDSTORAGEFILE object. Once you have an independent storage file object, you can use a standalone storage method to read, write, create, and delete files and file directory.

There is no way to prevent code to pass the code to obtain the code to pass the ISOLATEDSTORAGEFILE for the storage area without sufficient access. The domain and program set identification and independent storage permissions are only checked when obtaining reference to the ISOLATEDSTORAGE object (usually in the getUsersToreForasSembly, GetUsersToreFordomain or getStore method). Therefore, using these references should protect references to the ISOLATEDSTORAGEFILE object.

ObtainingaStore example

The following code example is an example of a very simple amount of storage area isolated from the user and the program. This code can be changed by retrieving the storage area that is isolated by the parameters passed to the GetStore method.

After running the code, you can confirm that the storage area has been created by typing StoreAdm / List on the command line. This will run a stand-alone store management tool (Storeadm.exe) and list all the currently all independent storage area.

[C #]

Using system;

Using system.io.IsolatedStorage;

Public class obtainingastore {

Public static void main () {

// Get a new isolated store for this assembly and put it into an

// isolated store Object.

IsolatedStorageFile isoStore = isolatedStorageFile.getStore (isolatedStorageScope.user | IsolatedStorageScope.assembly, null, null);

}

}

Enumerate storage area

You can use the ISOLATORAGEFILE Static method GetNumerator to enumerate all independent storage areas of the current user. GetEnumerator takes the isolatedStorageScope value and returns the number of isolatedStorageFile enumerations. User is the only supported ISOLATEDSTORAGESCOPE value. To enumerate the storage area, you must have an isolatedStorageFilePermission for specifying the isolatedStorageContainment value administerisolatedStorageByuser. When you use the ISOLATORAGESCOPE USER, GetEnumerator returns an ISOLATEDSTORAGEFILES array that is currently user-defined. ENUMERATINGSTORES example

The following code example obtains the storage area that is isolated by the user and the assembly and creates several files. Call the GetEnumerator method and put the result into the IEnumerator. The code then adds the size of the file in turn, and reports the result to the console. The actual enumeration occurred in a private enumerateThestore method. For the sake of clarity, the method is separated from the other parts of the code, placed on the bottom of the file.

[C #]

Using system;

Using system.io;

Using system.io.IsolatedStorage;

Using system.collections;

Public class enumeratingstores {

Public static int main () {

// Get An ISOLATED Store for this assembly and put it into an

// isolatedStorageFile Object.

IsolatedStorageFile isoStore = isolatedStorageFile.getStore (isolatedStorageScope.user | IsolatedStorageScope.assembly, null, null);

// this code create contents a few files so that the can be be enumerate.

IsolatedStorageFileStream Streama = New IsolatedStorageFileStream ("Testfilea.txt", FileMode.create, Isostore;

IsolatedStorageFileStream StreamB = New IsolatedStorageFileStream ("Testfileb.txt", FileMode.create, Isostore;

IsolatedStorageFileStream Streamc = New IsolatedStorageFileStream ("Testfilec.txt", FileMode.create, Isostore;

IsolatedStorageFileStream Streamd = New IsolatedStorageFileStream ("Testfile.txt", FileMode.create, Isostore;

streama.close ();

streamb.close ();

streamc.close ();

streamd.close ();

/// there might be a sales delay between when the Above code

// Executes and when the file... ..

// Closing and opening the store in this example ensures That // The Common Language Runtime Has Finished Creating The Files.

isostore .close ();

Isostore = isolatedStorageFile.getStore (isolatedStorageScope.user | isolatedStorageScope.assembly, null, null);

// this line of code calls a method at the bottom of the program

// That Puts all the files in isostore INTO An Ienumerator.

IEnumerator allfiles = enumeratethestore (isostore);

Long Totalsize = 0;

// this code counts up the sizes of all the stores.

While (allfiles .movenext ()) {

IsolatedStorageFile Store = (isolatedStorageFile) Allfiles.current;

Totalsize = (long) Store.currentsize;

}

Console.writeline ("THE TOTAL SIZE =" TOTALSIZE);

Return 0;

}

// this Method Returns An IenuContaining All The Files for a User.

Private static →umerator (isolatedStorageFile isoStore) {

IEnumerator E = isolatedStorageFile.GeteNumerator (IsolatedStorageScope.user);

Return E;

}

}

Delete store

IsolatedStorageFile provides two ways to delete independent storage files:

Example method Remove does not take any parameters, delete the storage area called it. This operation does not require any permissions. Any code that can access the storage area can delete any data or all data in the storage area.

The static method REMOVE uses the ISOLATEDSTORAGESCOPE value User and deletes all storage areas of the user running the code. This operation requires IsolatedStorageContainment value administerisolatedStorageByuser's ISOLATEDSTORAGEPERMISSITION permissions.

DELETINGSTORES example

The following code example demonstrates the use of static and instance REMOVE methods. Class get two stores, isolated on user and assembly; another by user, domain, and assembly. Delete the user, domain, and program set storage area by calling the REMOVE method of ISOLATEDSTORAGEFILE ISOSTORE1. Then, all remaining storage areas whose users are deleted by calling the static method ISOLATEDSTORAGE.REMOVE.

[C #]

Using system;

Using system.io.IsolatedStorage;

Public class delediningstores {

Public static void main () {

// Get a new isolated Store for this user, domain, and assembly.

// Put the store into an IsolatedStorageFile object.IsolatedStorageFile isoStore1 = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);

Console.writeline ("A Store isolated by User, Assembly, and Domain Has Been Obtained.");

// Get a new isolated store for user and assembly.

// Put That Store Into a Different IsolatedStorageFile Object.

IsolatedStorageFile isoStore2 = isolatedStorageFile.getStore (isolatedStorageScope.user | IsolatedStorageScope.assembly, null, null);

Console.writeline ("A Store isolated by user and ask.");

// The Remove Method deletes a Specific Store, in this case

// isostore1 file.

isostore1.remove ();

Console.writeline ("The User, Domain, And Assembly.");

// this Static Method deletes all the isolated Stores for this user.

IsolatedStorageFile.Remove (isolatedStorageScope.user);

Console.writeline ("All ISOLATED Stores for this User Have Been Deleted.");

} // end of main.

}

Situation of short-term space

Use independently stored code subject to quota limits, which specifies the maximum size of the data cabin where the independent storage file and the directory are located. This value is determined by the security policy, and the administrator can configure it. If you try to write data, you will exceed the maximum size allowed, will trigger ISOLATEDSTORAGEEXCEPTION and fail the operation. This helps to prevent malicious boycott service attacks, which will cause the application to reject the request due to this attack. To help you determine if a given write attempt fails because of this reason, the stand-alone store provides two read-only properties: isolatedStorage.currentsize and IsolatedStorage.maximumsize. These two attributes can be used to determine if the write storage area will result in the maximum size allowed exceeding the storage area. When you use these properties, remember that independent storage may be accessed simultaneously; therefore, if the amount of storage you calculate has the remainder, the storage space may be used when you try to write to the store. However, this does not hinder the maximum size of your storage area to determine whether the upper limit of the available storage will be reached.

Another important consideration is that the biggest small attribute depends on evidence from the assembly from normal work. Therefore, this method should only be called using getUsersToreForaSsembly (), getuserstorefordomain (),, getuserstorefordomain () or getStore (). The ISOLATEDSTORAGEFILE object created in any other way (eg, returning from getenumerator () will not return accurate maximum size. AnticipatingOutofspaceConditions example

The following code example obtains a stand-alone store, creates several files and metrically measure the remaining space in the storage area. Report the remaining space with bytes.

[C #]

Using system;

Using system.io;

Using system.io.IsolatedStorage;

Public class checkingspace {

Public static void main () {

// Get An ISOLATED Store for this assembly and put it into an

// isolatedStorefile Object.

IsolatedStorageFile isoStore = isolatedStorageFile.getStore (isolatedStorageScope.user | IsolatedStorageScope.assembly, null, null);

// CREATE A FEW PlaceHolder Files In The Isolated Store.

New isolatedStorageFileStream ("Intheroot.txt", filemode.create, isostore;

New isolatedStorageFileStream ("Another.txt", Filemode.create, Isostore;

New isolatedStorageFileStream ("Athird.txt", Filemode.create, Isostore;

New isolatedStorageFileStream ("AfouRTH.txt", Filemode.create, Isostore;

New isolatedStorageFileStream ("AfiffH.txt", FileMode.create, Isostore;

// use the currentsize and maximumsize methods to find remaining

// Space.

// Cast That Number Into a long Type and Put Into a variable.

LONG SPACEFT = (iSOStore.currentsize);

Console.writeline (Spaceleft "bytes of space.");

} // end of main.

}

Create files and directories

Once the store is obtained, you can create directories and files for storing data. In the storage area, the file name and directory name are specified relative to the root of the virtual file system.

To create a directory, use the ISOLATEDSTORAGEFILE method. If you specify a subdirectory that is not created, you will create two directories at the same time. If you specify an existing directory, it will not generate any exceptions. However, if you specify a directory name that contains an invalid character, IsolatedStorageException is generated. To create and open a file, use one of the ISOLATEDSTORAGESTREAM constructor, incoming file name, filemode value OpenorCreate, and store where you want to create a file. You can then perform an operation you want to perform on the file stream, such as reading, search, and writing. The IsolatedStorageFileStream constructor can also be used to open files for other purposes.

You can also create or open files without first obtaining a storage area without first obtaining a storage area. When using this form of constructor, the file is created in the domain storage area of ​​the file.

In the Windows file system, in order to compare the names, the independent storage files and directory names are not case sensitive. This way, if you have created a file called thisfile.txt, then create another file called thisfile.txt, actually only created a file. When displayed, the file name keeps its original case.

CreatingFileSAndDirectories example

The following code example illustrates how to create files and directories in standalone storage area. First, retrieve a memory area isolated by the user, domain, and program, and put it in the isostore variable. The CREATEDIRECTORY method is used to set a few different directories, and the ISOLATORAGEFILESTREAM method creates some files in these directories.

[C #]

Using system;

Using system.io;

Using system.io.IsolatedStorage;

Public class CreatingFileSDIRECTORIES {

Public static void main () {

// Get a new isolated Store for this user, domain, and assembly.

// Put The Store Into an isolatedStorageFile Object.

IsolatedStoragefile isoStore = isolatedStorageFile.getStore (isolatedStorageScope.user | IsolatedStorageScope.Domain | IsolatedStorageScope.assembly, null, null;

// This Cream Creates A Few DiffERENT DIRECTORIES.

Isostore.createdIRectory ("TopLevelDirectory";

Isostore.createdIRectory ("TopLevelDirectory / Secondlevel");

// this code Creates Two New Directories, One Inside The Other.

Isostore.createdIRectory ("AnothertopleVeldirectory / Insidedirectory);

// this file is placed in the root.

IsolatedStorageFileStream isostream1 = new isolatedStorageFileStream ("INTHEROOT.TXT", FileMode.create, Isostore; Console.writeline ("Created A New File In the root.");

isostream1.close ();

// this file is placed in the insidedomIRectory.

IsolatedStorageFileStream isostream2 = New isolatedStorageFileStream ("AnothertopleVeldirectory / Insidedirectory / Hereiam.txt", FileMode.create, Isostore;

isostream2.close ();

Console.writeline ("Created A New File In the Insidedirectory.");

} // end of main.

}

转载请注明原文地址:https://www.9cbs.com/read-28557.html

New Post(0)