コード例 #1
0
        public static string GetDataTime()
        {
            string str;
            IsolatedStorageFile isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain | IsolatedStorageScope.User, (Type)null, (Type)null);

            if (isf.GetDirectoryNames(UIConstants.IsolatedStorageDirectoryName).Length == 0)
            {
                return(string.Empty);
            }
            if (isf.GetFileNames(UIConstants.IsolatedStorage).Length == 0)
            {
                return(string.Empty);
            }
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.OpenOrCreate, isf))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    str = reader.ReadLine();
                }
            }
            if (!string.IsNullOrEmpty(str))
            {
                try
                {
                    str = EncodeHelper.DesDecrypt(str, UIConstants.IsolatedStorageEncryptKey);
                }
                catch
                {
                }
            }
            return(str);
        }
コード例 #2
0
        public static void SaveDataTime(DateTime fromDate)
        {
            IsolatedStorageFileStream stream2;
            StreamWriter writer;
            string       strText = fromDate.ToString("MM-dd-yyyy HH:mm:ss");
            string       str2    = GetDataTime().Trim();

            if (!string.IsNullOrEmpty(str2))
            {
                strText = str2 + ";" + strText;
            }
            strText = EncodeHelper.DesEncrypt(strText, UIConstants.IsolatedStorageEncryptKey);
            IsolatedStorageFile isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain | IsolatedStorageScope.User, (Type)null, (Type)null);

            string[] directoryNames          = isf.GetDirectoryNames(UIConstants.IsolatedStorageDirectoryName);
            IsolatedStorageFileStream stream = null;

            if (directoryNames.Length == 0)
            {
                isf.CreateDirectory(UIConstants.IsolatedStorageDirectoryName);
                using (stream2 = stream = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.Create, isf))
                {
                    using (writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(strText);
                    }
                    return;
                }
            }
            if (isf.GetFileNames(UIConstants.IsolatedStorage).Length == 0)
            {
                using (stream2 = stream = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.Create, isf))
                {
                    using (writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(strText);
                    }
                    return;
                }
            }
            using (stream2 = stream = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.Open, isf))
            {
                using (writer = new StreamWriter(stream))
                {
                    writer.WriteLine(strText);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// 从"独立存贮空间"取程序第一次运行的时间并解密
        /// </summary>
        /// <returns></returns>
        public static string GetDataTime()
        {
            string fromDataTime;

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

            string[] 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 (IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.OpenOrCreate, isoStore))
                {
                    using (StreamReader reader = new StreamReader(isoStream1))
                    {
                        fromDataTime = reader.ReadLine();
                    }
                }
                if (!string.IsNullOrEmpty(fromDataTime)) //解密
                {
                    try
                    {
                        fromDataTime = EncodeHelper.DesDecrypt(fromDataTime, UIConstants.IsolatedStorageEncryptKey);
                    }
                    catch
                    {
                    }
                }
                return(fromDataTime);
            }
        }
コード例 #4
0
        public object XmlDeserializeDecrypt(string path, Type type)
        {
            object obj3;

            try
            {
                string s = EncodeHelper.DecryptString(File.ReadAllText(path, Encoding.UTF8), true);
                using (MemoryStream stream = new MemoryStream(Encoding.Default.GetBytes(s)))
                {
                    XmlSerializer serializer = new XmlSerializer(type);
                    stream.Seek(0, SeekOrigin.Begin);
                    object obj2 = serializer.Deserialize(stream);
                    stream.Close();
                    obj3 = obj2;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                obj3 = null;
            }
            return(obj3);
        }
コード例 #5
0
        public bool XmlSerializeEncrypt(string path, object obj, Type type)
        {
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            namespaces.Add("", "");
            try
            {
                if (!File.Exists(path))
                {
                    FileInfo info = new FileInfo(path);
                    if (!info.Directory.Exists)
                    {
                        Directory.CreateDirectory(info.Directory.FullName);
                    }
                }
                using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    string input = "";
                    using (MemoryStream stream2 = new MemoryStream())
                    {
                        new XmlSerializer(type).Serialize(stream2, obj, namespaces);
                        stream2.Seek(0, SeekOrigin.Begin);
                        input = Encoding.ASCII.GetString(stream2.ToArray());
                    }
                    string s     = EncodeHelper.EncryptString(input);
                    byte[] bytes = Encoding.Default.GetBytes(s);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                }
                return(true);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                return(false);
            }
        }
コード例 #6
0
        /// <summary>
        /// 加密并保存当前时间到"独立存贮空间" (以分号(;)追加保存)
        /// </summary>
        public static void SaveDataTime(DateTime fromDate)
        {
            string fromDataTime = fromDate.ToString("MM-dd-yyyy HH:mm:ss");
            string oldTime      = GetDataTime().Trim();

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

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

            string username = fromDataTime;
            //按用户、域、程序集获取独立存储区
            IsolatedStorageFile isoStore =
                IsolatedStorageFile.GetStore(
                    IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);
            string[] 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 (StreamWriter 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 (StreamWriter writer = new StreamWriter(isoStream1))
                        {
                            writer.WriteLine(fromDataTime);
                        }
                    }
                }
                else
                {
                    using (isoStream1 = new IsolatedStorageFileStream(UIConstants.IsolatedStorage, FileMode.Open, isoStore))
                    {
                        //写入文件
                        using (StreamWriter writer = new StreamWriter(isoStream1))
                        {
                            writer.WriteLine(fromDataTime);
                        }
                    }
                }
            }

            #endregion
        }