using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.IO;
using Microsoft.Win32.SafeHandles;
namespace IsolatedStorageFileStreamDemo
{
class Program
{
static void Main(string[] args)
{
IsolatedStorageFile isoFile =
IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain,
typeof(System.Security.Policy.Url), typeof(System.Security.Policy.Url));
string[] Directorysz = isoFile.GetDirectoryNames();
string[] fileNames = isoFile.GetFileNames();
foreach (string directory in Directorysz)
{
//isoFile.DeleteDirectory(directory);
Console.WriteLine(directory);
}
foreach (string s in fileNames)
{
//isoFile.DeleteFile(s);
Console.WriteLine(s);
}
Console.ReadKey();
}
}
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class LoginPrefs
{
public string NewsUrl { get; set; }
public string SportsUrl { get; set; }
public string userName { get; set; }
private bool myNewPrefs;
public bool NewPrefs
{
get { return myNewPrefs; }
}
public LoginPrefs(string path)
{
this.NewsUrl = path;
myNewPrefs = GetPrefsForUser();
}
private bool GetPrefsForUser()
{
try
{
//在这个程序集和域检索程序私人存储
IsolatedStorageFile iosFile =
IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);
IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(this.NewsUrl, FileMode.Open, FileAccess.Read, FileShare.Read);
// The code executes to this point only if a file corresponding to the username exists.代码执行这一点只有一个文件对应于用户名存在
// Though you can perform operations on the stream, you cannot get a handle to the file.虽然你可以执行操作流,你不能得到一个处理文件
try
{
SafeFileHandle aFileHandle = isoStream.SafeFileHandle;
Console.WriteLine("A pointer to a file handle has been obtained(一个指向文件的句柄)" + aFileHandle.ToString());
Console.WriteLine("哈希值" + aFileHandle.GetHashCode().ToString());
}
catch (Exception e)
{
Console.WriteLine("获取句柄错误" + e.Message);
}
StreamReader streamReader = new StreamReader(isoStream);
this.NewsUrl = streamReader.ReadLine();
this.SportsUrl = streamReader.ReadLine();
throw new FileNotFoundException();
streamReader.Close();
iosFile.Close();
return false;
}
catch (FileNotFoundException)
{
return true;
}
}
public bool GetIsoStoreInfo()
{
// Get a User store with type evidence for the current Domain and the Assembly.
IsolatedStorageFile isoFile =
IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain,
typeof(System.Security.Policy.Url), typeof(System.Security.Policy.Url));
string[] dirNames = isoFile.GetDirectoryNames();
string[] fileNames = isoFile.GetFileNames();
// List directories currently in this Isolated Storage.
if (dirNames.Length > 0)
{
for (int i = 0; i < dirNames.Length; ++i)
{
Console.WriteLine("Directory Name: " + dirNames[i]);
}
}
// List the files currently in this Isolated Storage.
// The list represents all users who have personal preferences stored for this application.
if (fileNames.Length > 0)
{
for (int i = 0; i < fileNames.Length; ++i)
{
Console.WriteLine("File Name: " + fileNames[i]);
}
}
isoFile.Close();
return true;
}
/// <summary>
/// 创建独立内存 写入文件目录和文件
/// </summary>
/// <returns></returns>
public double SetPrefsForUser()
{
try
{
IsolatedStorageFile isoFile;
isoFile = IsolatedStorageFile.GetUserStoreForDomain();
IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(this.userName, FileMode.OpenOrCreate, FileAccess.Write, isoFile);
StreamWriter writer = new StreamWriter(isoStream);
writer.WriteLine(this.NewsUrl);
writer.WriteLine(this.SportsUrl);
double d = isoFile.CurrentSize / isoFile.MaximumSize;
Console.WriteLine("剩下独立内存{0}", isoFile.CurrentSize);
Console.WriteLine("独立总共内存{0}", isoFile.MaximumSize);
writer.Close();
isoFile.Dispose();
isoFile.Close();
return d;
}
catch (IsolatedStorageException e)
{
Console.WriteLine(e);
return 0.0;
}
}
/// <summary>
/// 删除独立内存文件
/// </summary>
public void DeleteFiles()
{
try
{
IsolatedStorageFile isoFile =
IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain,
typeof(System.Security.Policy.Url), typeof(System.Security.Policy.Url));
string[] dirFile = isoFile.GetDirectoryNames();
string[] fileNames = isoFile.GetFileNames();
if (fileNames.Length > 0)
{
for (int i = 0; i < fileNames.Length; i++)
{
isoFile.DeleteFile(fileNames[i]);
}
fileNames = isoFile.GetFileNames("*");
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
/// <summary>
/// 删除独立下面的目录和文件
/// </summary>
public void DeleteDirectories()
{
try
{
IsolatedStorageFile isoFile =
IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain,
typeof(System.Security.Policy.Url), typeof(System.Security.Policy.Url));
string[] Directoryd = isoFile.GetDirectoryNames();
string[] FileNames = isoFile.GetFileNames();
if (FileNames.Length > 0)
{
foreach (string name in FileNames)
{
isoFile.DeleteFile(name);
}
}
if (Directoryd.Length > 0)
{
foreach (string dir in Directoryd)
{
isoFile.DeleteDirectory(dir);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public double SetNewPrefsForUser()
{
try
{
byte inputChar;
IsolatedStorageFile isoFile =
IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly,
typeof(System.Security.Policy.Url), typeof(System.Security.Policy.Url));
if (!this.myNewPrefs)
{
if (isoFile.GetDirectoryNames("Archive").Length == 0)
{
isoFile.CreateDirectory("Archive");
}
else
{
IsolatedStorageFileStream soure = new IsolatedStorageFileStream(this.userName, FileMode.OpenOrCreate, isoFile);
//This is the stream from which data will be read(这是流,数据将被阅读)
Console.WriteLine("Is the soure file readable?" + (soure.CanRead ? "true" : "false"));
IsolatedStorageFileStream target = new IsolatedStorageFileStream("Archive\\" + this.userName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, isoFile);
Console.WriteLine("Is the target file witable?" + (target.CanRead ? "true" : "false"));
//Stream the old file to a new file in the Archive directory.(流旧的文件到一个新的文件在存档目录)
if (soure.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 (soure.Position < soure.Length)
{
inputChar = (byte)soure.ReadByte();
target.WriteByte(inputChar);
}
// Determine the size of the IsolatedStorageFileStream
// by checking its Length property.
Console.WriteLine("Total Bytes Read:" + soure.Length);
}
// After you have read and written to the streams, close them.
target.Close();
soure.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.
StreamWriter writer = new StreamWriter(isoStream);
// Update the data based on the new inputs.
writer.WriteLine(this.NewsUrl);
writer.WriteLine(this.SportsUrl);
// Calculate the amount of space used to record this user's preferences.
double d = isoFile.CurrentSize / isoFile.MaximumSize;
Console.WriteLine("CurrentSize = " + isoFile.CurrentSize.ToString());
Console.WriteLine("MaximumSize = " + isoFile.MaximumSize.ToString());
// StreamWriter.Close implicitly closes isoStream.
writer.Close();
isoFile.Close();
return d;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return 0.0;
}
}
}
}