zoukankan      html  css  js  c++  java
  • 关于用"独立存贮空间"存数据存在的问题

    这个行不通,我写的方法如下:
    /// <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;
    }
    }

    结果是:两个人用时就会"交叉感染"
  • 相关阅读:
    2014第16周六
    2014第16周五
    2014第16周四
    2014第16周三CSS布局再学习摘录
    2014第16周二
    2014第16周一
    2014第15周日
    2014第15周六
    2014第15周五
    SDN:软件定义网络
  • 原文地址:https://www.cnblogs.com/cerxp/p/5153.html
Copyright © 2011-2022 走看看