/* Attempts to open the file at filepath and read the XML */ public WatchedPath ReadFile(string filepath) { XmlTextReader textReader = new XmlTextReader(filepath); WatchedPath path = new WatchedPath(); // Read in the data while (textReader.Read()) { if (textReader.NodeType == XmlNodeType.Element && textReader.Name == "Start") { path.Path = textReader.GetAttribute("path"); while (textReader.NodeType != XmlNodeType.EndElement) { textReader.Read(); if (textReader.Name == "Destination") { path.Destinations.Add(textReader.GetAttribute("path")); } } } } // Close the reader textReader.Close(); return path; }
public SystemTrayForm() { // Initialize private members m_tray_menu = new ContextMenu(); m_tray_icon = new NotifyIcon(); m_file_watcher = new FileSystemWatcher(); m_watched_path = new WatchedPath(); // Set up the system tray menu m_tray_menu.MenuItems.Add("Sync folder", OnSyncFolderSelect); m_tray_menu.MenuItems.Add("Watched folder", OnWatchedFolderSelect); m_tray_menu.MenuItems.Add("-"); m_tray_menu.MenuItems.Add("Exit", OnExit); m_tray_icon.Text = "AutoSync"; m_tray_icon.Icon = new Icon(SystemIcons.Application, 40, 40); m_tray_icon.ContextMenu = m_tray_menu; m_tray_icon.Visible = true; try { ReadWatchedPath(); if (m_watched_path != null) { m_file_watcher.Path = m_watched_path.Path; } } catch (Exception e) { // Do something? } /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ m_file_watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Add event handlers. m_file_watcher.Changed += new FileSystemEventHandler(OnChanged); m_file_watcher.Created += new FileSystemEventHandler(OnChanged); m_file_watcher.Deleted += new FileSystemEventHandler(OnDeleted); m_file_watcher.Renamed += new RenamedEventHandler(OnRenamed); // Begin watching (if possible). SwitchFileWatcher(true); }
public bool WriteFile(string filepath, WatchedPath path) { XmlTextWriter textWriter = new XmlTextWriter(filepath, null); // Write the start of the document textWriter.WriteStartDocument(); // Write the starting name textWriter.WriteStartElement("Start"); textWriter.WriteAttributeString("path", path.Path); foreach (string dest in path.Destinations) { textWriter.WriteStartElement("Destination"); textWriter.WriteAttributeString("path", dest); textWriter.WriteEndElement(); } textWriter.WriteEndElement(); // End the document and close textWriter.WriteEndDocument(); textWriter.Close(); return true; }
/* Retrieve the path stored in the paths.sav file */ protected void ReadWatchedPath() { FileSyncXMLParser parser = new FileSyncXMLParser(); m_watched_path = parser.ReadFile(SAVE_FILE_NAME); }