예제 #1
0
        /// <summary>
        /// 从"独立存贮空间"取程序第一次运行的时间并解密
        /// </summary>
        /// <returns></returns>
        public static string GetDataTime()
        {
            string fromDataTime;

            //按用户、域、程序集获取独立存储区
            var isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User
                                                        | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);

            var myusername = isoStore.GetDirectoryNames(UiConstants.IsolatedStorageDirectoryName);

            if (myusername.Length == 0) //没有文件夹
            {
                return(string.Empty);   //域中没有他的目录
            }

            myusername = isoStore.GetFileNames(UiConstants.IsolatedStorage);
            if (myusername.Length == 0) //没有文件
            {
                return(string.Empty);   //域中没有他的用户名
            }
            else
            {
                using (var isoStream1 = new IsolatedStorageFileStream(UiConstants.IsolatedStorage, FileMode.OpenOrCreate, isoStore))
                {
                    using (var reader = new StreamReader(isoStream1))
                    {
                        fromDataTime = reader.ReadLine();
                    }
                }
                if (!string.IsNullOrEmpty(fromDataTime)) //解密
                {
                    try
                    {
                        fromDataTime = DesEncrypt.Decrypt(fromDataTime, UiConstants.IsolatedStorageEncryptKey);
                    }
                    catch
                    {
                    }
                }
                return(fromDataTime);
            }
        }
예제 #2
0
        /// <summary>
        /// 加密并保存当前时间到"独立存贮空间" (以分号(;)追加保存)
        /// </summary>
        public static void SaveDataTime(DateTime fromDate)
        {
            var fromDataTime = fromDate.ToString("MM-dd-yyyy HH:mm:ss");
            var oldTime      = GetDataTime().Trim();

            if (!string.IsNullOrEmpty(oldTime))
            {
                fromDataTime = oldTime + ";" + fromDataTime;                                        //追加最后时间到左边
            }
            fromDataTime = DesEncrypt.Encrypt(fromDataTime, UiConstants.IsolatedStorageEncryptKey); //加密

            #region 将fromDataTime保存在"独立存贮空间"

            var username = fromDataTime;
            //按用户、域、程序集获取独立存储区
            var isoStore =
                IsolatedStorageFile.GetStore(
                    IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);
            var myusername = isoStore.GetDirectoryNames(UiConstants.IsolatedStorageDirectoryName);
            IsolatedStorageFileStream isoStream1 = null;
            if (myusername.Length == 0) //没有目录
            {
                //创建目录
                isoStore.CreateDirectory(UiConstants.IsolatedStorageDirectoryName);
                //创建文件
                using (isoStream1 = new IsolatedStorageFileStream(UiConstants.IsolatedStorage, FileMode.Create, isoStore))
                {
                    //写入文件
                    using (var writer = new StreamWriter(isoStream1))
                    {
                        writer.WriteLine(fromDataTime);
                    }
                }
            }
            else
            {
                myusername = isoStore.GetFileNames(UiConstants.IsolatedStorage);
                if (myusername.Length == 0) //没有文件
                {
                    //创建文件
                    using (isoStream1 = new IsolatedStorageFileStream(UiConstants.IsolatedStorage, FileMode.Create, isoStore))
                    {
                        //写入文件
                        using (var writer = new StreamWriter(isoStream1))
                        {
                            writer.WriteLine(fromDataTime);
                        }
                    }
                }
                else
                {
                    using (isoStream1 = new IsolatedStorageFileStream(UiConstants.IsolatedStorage, FileMode.Open, isoStore))
                    {
                        //写入文件
                        using (var writer = new StreamWriter(isoStream1))
                        {
                            writer.WriteLine(fromDataTime);
                        }
                    }
                }
            }

            #endregion
        }