Exemplo n.º 1
0
        public void SaveFile(string filename)
        {
            bool   success      = false;
            string errorMessage = "";

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

                WriteLog($"Save File To: {filename}");

                string        systemTempDirectory = Path.GetTempPath();
                DirectoryInfo saveDirectory       = new DirectoryInfo(Path.Combine(systemTempDirectory, tempDirectory));

                if (saveDirectory.Exists)
                {
                    saveDirectory.Delete(true);
                }

                WriteLog($"Temp Directory: {saveDirectory.FullName}");

                foreach (var watcher in Watchers)
                {
                    string watcherCache = Path.Combine(systemTempDirectory, watcher.WebView.Engine.CachePath);
                    FileHelper.DirectoryCopy(watcherCache, Path.Combine(saveDirectory.FullName, watcher.WebView.Engine.CachePath));
                }

                WriteLog($"Cache Copied");

                SaveSession(saveDirectory.FullName);

                ZipFile.CreateFromDirectory(saveDirectory.FullName, filename, CompressionLevel.Optimal, false);

                WriteLog($"File Created");

                if (saveDirectory.Exists)
                {
                    saveDirectory.Delete(true);
                }

                WriteLog($"Temp Directory Removed. Save Done!");

                success = true;
            }
            catch (Exception ex)
            {
                WriteLog($"Save Error: {ex.Message}");
                success      = false;
                errorMessage = ex.Message;
            }
            finally
            {
                WatcherManagerFileOperationEvent?.Invoke(this, new WatcherManagerFileOperationEventArgs(FileOperation.SAVE, success, errorMessage));
            }
        }
Exemplo n.º 2
0
        public void OpenFile(string filename)
        {
            bool   success      = false;
            string errorMessage = "";

            try
            {
                WriteLog($"Open File - {filename}");

                Clear();

                if (!File.Exists(filename))
                {
                    string message = $"File {filename} Not Exists";
                    WriteLog(message);
                    errorMessage = message;
                    success      = false;
                    return;
                }

                string openDirectory = Path.Combine(Path.GetTempPath(), tempDirectory);

                if (Directory.Exists(openDirectory))
                {
                    Directory.Delete(openDirectory, true);
                }

                ZipFile.ExtractToDirectory(filename, openDirectory);

                XmlDocument session = new XmlDocument();
                session.Load(Path.Combine(openDirectory, sessionFileName));
                XmlElement root = session.DocumentElement;
                foreach (XmlNode watcher in root.ChildNodes)
                {
                    try
                    {
                        string id       = GetAttributeValue <string>(watcher, "id");
                        string url      = GetAttributeValue <string>(watcher, "url");
                        bool   logged   = GetAttributeValue <bool>(watcher, "logged");
                        string login    = GetAttributeValue <string>(watcher, "login");
                        string password = GetAttributeValue <string>(watcher, "password");

                        var src = Path.Combine(Path.GetTempPath(), tempDirectory, id);
                        if (Directory.Exists(src))
                        {
                            string dst = Path.Combine(Path.GetTempPath(), id);
                            if (Directory.Exists(dst))
                            {
                                Directory.Delete(dst, true);
                            }

                            FileHelper.DirectoryCopy(src, dst);

                            Watcher w = new Watcher(id, url, logged, login, password);

                            Application.Current.Dispatcher.Invoke(() => watchers.Add(w));
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteLog($"Create Watcher Error: {ex.Message}");
                        continue;
                    }
                }
                Directory.Delete(openDirectory, true);

                WriteLog($"File Loaded {filename}");
                success      = true;
                WatcherCount = watchers.Count;
            }
            catch (Exception ex)
            {
                string message = $"Open file error {ex.Message}";
                WriteLog(message);
                success      = false;
                errorMessage = message;
            }
            finally
            {
                WatcherManagerFileOperationEvent?.Invoke(this, new WatcherManagerFileOperationEventArgs(FileOperation.LOAD, success, errorMessage));
            }
        }