Beispiel #1
0
        internal void Save(XFile file)
        {
            if (file == null)
            {
                return;
            }

            lock (GetLock())
            {
                ChangeLock(file, FileCacheStatus.Locked);

                if (!string.IsNullOrEmpty(this.password))
                {
                    StringBuilder builder = new StringBuilder();
                    using (TextWriter writer = new StringWriter(builder))
                        file.Document.Save(writer);
                    FileCryptoHelper.EncryptContent(builder.ToString(), file.Path, this.password);
                }
                else
                {
                    using (FileStream fs = new FileStream(file.Path, FileMode.Create, FileAccess.Write, FileShare.Read)) {
                        file.Document.Save(fs);
                    }
                }

                ChangeLock(file, FileCacheStatus.Shared);
            }
        }
Beispiel #2
0
        internal XFile Load(Type type, string path, string password = null)
        {
            XFile file = null;

            if (System.IO.File.Exists(path))
            {
                //no need for lock(), it is been called by lock-enabled method
                if (!string.IsNullOrEmpty(password))
                {
                    try
                    {
                        lock (GetLock())
                        {
                            XDocument doc = XDocument.Parse(FileCryptoHelper.DecryptContent(path, password));
                            file = new XFile(doc, type, path, password);
                        }
                    }
                    catch
                    {
                        //exceptionService.Throw(new SecurityException());
                    }
                }
                else
                {
                    lock (GetLock())
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            file = new XFile(XDocument.Load(fs), type, path, password);
                        }
                    }
                }
            }

            return(file);
        }