这个行不通,我写的方法如下:
/// <summary>
/// 将username保存在"独立存贮空间"
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public void SaveUserName(string fromusername)
{
#region 将username保存在"独立存贮空间"
string username=fromusername;
//按用户、域、程序集获取独立存储区
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User|IsolatedStorageScope.Domain|IsolatedStorageScope.Assembly, null, null);
string[] myusername=isoStore.GetDirectoryNames("UserNameDir");
IsolatedStorageFileStream isoStream1=null;
if(myusername.Length==0)//没有目录
{
//创建目录
isoStore.CreateDirectory("UserNameDir");
//创建文件
isoStream1 = new IsolatedStorageFileStream("UserNameDir//UserName.txt", FileMode.Create, isoStore);
//写入文件
StreamWriter writer = null;
writer = new StreamWriter(isoStream1);
writer.WriteLine(fromusername);
writer.Close();
isoStream1.Close();
}
else
{
myusername=isoStore.GetFileNames("UserNameDir//UserName.txt");
if(myusername.Length==0)//没有文件
{
//创建文件
isoStream1 = new IsolatedStorageFileStream("UserNameDir//UserName.txt", FileMode.Create, isoStore);
//写入文件
StreamWriter writer = null;
writer = new StreamWriter(isoStream1);
writer.WriteLine(fromusername);
writer.Close();
isoStream1.Close();
}
else
{
isoStream1 = new IsolatedStorageFileStream("UserNameDir//UserName.txt", FileMode.OpenOrCreate, isoStore);
//写入文件
StreamWriter writer = null;
writer = new StreamWriter(isoStream1);
writer.Write(fromusername);
writer.Close();
isoStream1.Close();
}
}
#endregion
}
读的代码如下:
/// <summary>
/// 从"独立存贮空间"取username
/// </summary>
/// <returns></returns>
public string GetUserName()
{
string fromusername;
//按用户、域、程序集获取独立存储区
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User|IsolatedStorageScope.Domain|IsolatedStorageScope.Assembly, null, null);
string[] myusername=isoStore.GetFileNames("UserNameDir//UserName.txt");
if(myusername.Length==0)//没有文件
{
return null;//域中没有他的用户名
}
else
{
IsolatedStorageFileStream isoStream1=null;
isoStream1 = new IsolatedStorageFileStream("UserNameDir//UserName.txt", FileMode.OpenOrCreate, isoStore);
StreamReader reader =null;
reader=new StreamReader(isoStream1);
fromusername=reader.ReadLine();
reader.Close();
return fromusername;
}
}
结果是:两个人用时就会"交叉感染"
/// <summary>
/// 将username保存在"独立存贮空间"
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public void SaveUserName(string fromusername)
{
#region 将username保存在"独立存贮空间"
string username=fromusername;
//按用户、域、程序集获取独立存储区
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User|IsolatedStorageScope.Domain|IsolatedStorageScope.Assembly, null, null);
string[] myusername=isoStore.GetDirectoryNames("UserNameDir");
IsolatedStorageFileStream isoStream1=null;
if(myusername.Length==0)//没有目录
{
//创建目录
isoStore.CreateDirectory("UserNameDir");
//创建文件
isoStream1 = new IsolatedStorageFileStream("UserNameDir//UserName.txt", FileMode.Create, isoStore);
//写入文件
StreamWriter writer = null;
writer = new StreamWriter(isoStream1);
writer.WriteLine(fromusername);
writer.Close();
isoStream1.Close();
}
else
{
myusername=isoStore.GetFileNames("UserNameDir//UserName.txt");
if(myusername.Length==0)//没有文件
{
//创建文件
isoStream1 = new IsolatedStorageFileStream("UserNameDir//UserName.txt", FileMode.Create, isoStore);
//写入文件
StreamWriter writer = null;
writer = new StreamWriter(isoStream1);
writer.WriteLine(fromusername);
writer.Close();
isoStream1.Close();
}
else
{
isoStream1 = new IsolatedStorageFileStream("UserNameDir//UserName.txt", FileMode.OpenOrCreate, isoStore);
//写入文件
StreamWriter writer = null;
writer = new StreamWriter(isoStream1);
writer.Write(fromusername);
writer.Close();
isoStream1.Close();
}
}
#endregion
}
读的代码如下:
/// <summary>
/// 从"独立存贮空间"取username
/// </summary>
/// <returns></returns>
public string GetUserName()
{
string fromusername;
//按用户、域、程序集获取独立存储区
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User|IsolatedStorageScope.Domain|IsolatedStorageScope.Assembly, null, null);
string[] myusername=isoStore.GetFileNames("UserNameDir//UserName.txt");
if(myusername.Length==0)//没有文件
{
return null;//域中没有他的用户名
}
else
{
IsolatedStorageFileStream isoStream1=null;
isoStream1 = new IsolatedStorageFileStream("UserNameDir//UserName.txt", FileMode.OpenOrCreate, isoStore);
StreamReader reader =null;
reader=new StreamReader(isoStream1);
fromusername=reader.ReadLine();
reader.Close();
return fromusername;
}
}
结果是:两个人用时就会"交叉感染"