public bool SaveToFile(string filename, string password)
        {
            try
            {
                //Make a copy of the original file if the original file is above 1 kb
                if (File.Exists(filename) && SystemIOHelper.GetFileSize(filename) > 1024)
                {
                    string copyFilename = GeneralConverters.GetDirectoryNameFromPath(filename, true) + "BookmarksCopy.dat";
                    if (File.Exists(copyFilename))
                    {
                        File.Delete(copyFilename);
                    }
                    File.Copy(filename, copyFilename);
                }

                var  settings       = new StorageManagerSettings(false, Environment.ProcessorCount, true, password);
                var  storageManager = new StorageManager(settings);
                bool successful     = storageManager.SerializeObjectToFile(_bookmarkContainer, filename, null);

                if (successful)
                {
                    IsModified = false;
                }

                return(successful);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "BookmarkManager.SaveToFile(string filename, string password) : " + ex.Message, ex);
                return(false);
            }
        }
示例#2
0
 private void Initialize()
 {
     settingsFilePath      = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + Assembly.GetExecutingAssembly().GetName().Name + "\\SecureChatAppSettings.dat";
     settingsDirectoryPath = GeneralConverters.GetDirectoryNameFromPath(settingsFilePath);
     try
     {
         if (!Directory.Exists(settingsDirectoryPath))
         {
             Directory.CreateDirectory(settingsDirectoryPath);
         }
     }
     catch (Exception ex)
     {
         LogWriter.WriteLog(ex.Message);
     }
 }
        public Task <int> FixBrokenLinksFromBaseDir(string selectedPath)
        {
            var task = Task <int> .Factory.StartNew(() =>
            {
                int filePathsCorrected = 0;
                var brokenLinksList    = GetAllBookmarksWithIncorrectPath(RootFolder);

                foreach (var bookmark in brokenLinksList)
                {
                    var fileMatches = Directory.EnumerateFiles(selectedPath, bookmark.FileName, SearchOption.AllDirectories).ToList();

                    if (fileMatches.Any())
                    {
                        foreach (string fileMatch in fileMatches)
                        {
                            FileInfo fileInfo = new FileInfo(fileMatch);
                            if (fileInfo.Length == bookmark.Size)
                            {
                                string dir      = GeneralConverters.GetDirectoryNameFromPath(fileMatch);
                                string fileName = GeneralConverters.GetFileNameFromPath(fileMatch);

                                bookmark.Directory      = dir;
                                bookmark.FileName       = fileName;
                                bookmark.CompletePath   = fileMatch;
                                bookmark.CreationTime   = fileInfo.CreationTime;
                                bookmark.LastAccessTime = fileInfo.LastAccessTime;
                                bookmark.LastWriteTime  = fileInfo.LastWriteTime;
                                filePathsCorrected++;
                                break;
                            }
                        }
                    }
                }

                return(filePathsCorrected);
            });

            return(task);
        }
示例#4
0
        public void RecreateDatabase(List <ThumbnailEntry> thumbnailEntries)
        {
            if (_fileStream == null)
            {
                lock (_fileOperationLock)
                {
                    _fileStream = File.Open(_fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                }
            }
            else
            {
                SaveToDisk();
            }

            string tempFileName = GeneralConverters.GetDirectoryNameFromPath(_fileName) + TemporaryDatabaseFilename;

            FileStream temporaryDatabaseFile = null;

            try
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }

                // Verify
                var deleteQueue = new Queue <ThumbnailEntry>();
                foreach (var thumbnailEntry in thumbnailEntries)
                {
                    if (thumbnailEntry.Length <= 0 || !File.Exists(Path.Combine(thumbnailEntry.Directory, thumbnailEntry.FileName)))
                    {
                        deleteQueue.Enqueue(thumbnailEntry);
                    }
                }

                while (deleteQueue.Count > 0)
                {
                    thumbnailEntries.Remove(deleteQueue.Dequeue());
                }

                temporaryDatabaseFile = File.OpenWrite(tempFileName);
                foreach (ThumbnailEntry entry in thumbnailEntries)
                {
                    var buffer = new byte[entry.Length];
                    lock (_fileOperationLock)
                    {
                        _fileStream.Position = entry.FilePosition;
                        _fileStream.Read(buffer, 0, entry.Length);
                    }

                    entry.FilePosition = temporaryDatabaseFile.Position;
                    temporaryDatabaseFile.Write(buffer, 0, entry.Length);
                }

                CloseStream();
                temporaryDatabaseFile.Flush(true);
                temporaryDatabaseFile.Close();
                temporaryDatabaseFile = null;
                File.Delete(_fileName);
                File.Move(tempFileName, _fileName);
            }
            catch (Exception exception)
            {
                Log.Error(exception, "Error in RecreateDatabase()");
            }
            finally
            {
                temporaryDatabaseFile?.Close();
                lock (_fileOperationLock)
                {
                    _fileStream?.Close();
                    _fileStream = null;
                }
            }
        }