// Shared variable: text_buffer_sv
        // This is shared between the background thread that is
        // generating the text, and the GUI thread that is
        // displaying the text.



        #region Loading and saving settings

        public WindowsBackup_App(TextBox Output_tb, Forms.NotifyIcon notify_icon,
                                 Icon ready, Icon busy, Icon error)
        {
            this.Output_tb   = Output_tb;
            this.notify_icon = notify_icon;
            ready_icon       = ready;
            busy_icon        = busy;
            error_icon       = error;

            if (File.Exists(settings_path) == false)
            {
                create_default_file();

                // Dispose the objects so to recreate from file later
                file_name_reg.Dispose();
            }

            try
            {
                // The "delay_thread" needs to be constructed first, before calling "read_settings()".
                // The reason is that if there is an error inside "read_settings()",
                // then the "delay_thread" is used to write messages to the "Output_tb".
                delay_thread = new DelayThread(Output_tb, new Func_void(add_text_to_Output_tb));

                read_settings();

                backup_manager.start_live_backup();
            }
            catch (Exception ex)
            {
                handle_event(AppEventType.ERROR, new object[] { ex.Message });
            }
        }
예제 #2
0
        /// <summary>
        /// Tests the FileNameRegistration class. Retrieve names for some
        /// files, delete nodes, add nodes, and perform a compact operation.
        /// </summary>
        public void test_FileNameRegistration()
        {
            Console.WriteLine("About to test the FileNameRegistration. Attempt to get a "
                              + "few alternative file names. Then delete some files to trigger "
                              + "compaction.  Press any key to continue, or ESC to skip.");
            if (hit_esc_key())
            {
                return;
            }

            // Add files to the file name registration tree.
            var file_name_reg = new FileNameRegistration(file_name_reg_xml);

            Console.WriteLine();
            Console.WriteLine("The test now creates a basic directory structure and "
                              + "print information about it.");

            // Create a basic directory structure.
            var path_status = file_name_reg.get_path_status(@"E:\a");

            if (path_status == null)
            {
                // .add_file(...) can only happen once per file, so this block
                // should run just once.
                Console.WriteLine(file_name_reg.add_file(@"E:\a"));
                Console.WriteLine(file_name_reg.add_file(@"E:\a1\b"));
                Console.WriteLine(file_name_reg.add_file(@"E:\a1\c"));

                Console.WriteLine(file_name_reg.add_file(@"E:\a1\c2\cc2"));
                Console.WriteLine(file_name_reg.add_file(@"E:\a1\c2\cc2b"));
                Console.WriteLine(file_name_reg.add_file(@"E:\a1\c2\cc2c"));
                Console.WriteLine(file_name_reg.add_file(@"E:\a1\c2\cc2dd\c2a"));
                Console.WriteLine(file_name_reg.add_file(@"E:\a1\c2\cc2dd\c2b"));

                Console.WriteLine(file_name_reg.add_file(@"E:\a1\c3"));
            }

            // print how things look so far
            file_name_reg.print();
            file_name_reg.print(@"E:\temp\temp\fnr.txt");
            Console.WriteLine();

            // Test the .get_path_status(...)
            string[] paths = { @"E:\a1\c2", @"E:\a1\b", @"E:\a1\no_such_thing" };
            foreach (string path in paths)
            {
                var path_info = file_name_reg.get_path_status(path);
                if (path_info == null)
                {
                    Console.WriteLine(path + " does not exist.");
                }
                else
                {
                    Console.WriteLine(path);
                    Console.WriteLine("is_file = " + path_info.is_file
                                      + "; alt_file_name = " + path_info.alt_file_name
                                      + "; modified_time = " + path_info.modified_time);
                }
                Console.WriteLine();
            }

            // Test the .get_names(...)
            List <string> sub_dir_names = null, file_names = null, alt_file_names = null;
            string        path2 = @"E:\a1\c2";

            file_name_reg.get_names(path2, ref sub_dir_names, ref file_names, ref alt_file_names);

            if (sub_dir_names != null)
            {
                Console.Write(path2 + " sub directory names: ");
                foreach (var name in sub_dir_names)
                {
                    Console.Write(name + ", ");
                }
                Console.WriteLine();
            }

            if (file_names != null)
            {
                Console.Write(path2 + " file names: ");
                foreach (var name in file_names)
                {
                    Console.Write(name + ", ");
                }
                Console.WriteLine();
            }

            if (alt_file_names != null)
            {
                Console.Write(path2 + " alternative file names: ");
                foreach (var name in alt_file_names)
                {
                    Console.Write(name + ", ");
                }
                Console.WriteLine();
            }

            pause();

            // Delete
            Console.WriteLine(@"Delete E:\a1\c2");
            file_name_reg.delete(@"E:\a1\c2");
            file_name_reg.print();
            pause();

            file_name_reg.Dispose();

            // Reload from file to trigger compaction
            file_name_reg = new FileNameRegistration(file_name_reg_xml);

            // Get modified time
            Console.WriteLine("Test changing the modified time.");
            Console.WriteLine(@"E:\a1\b modified time = " +
                              file_name_reg.get_modified_time(@"E:\a1\b"));

            // Set modified time
            file_name_reg.set_modified_time(@"E:\a1\b", DateTime.Now);
            Console.WriteLine(@"E:\a1\b modified time = " +
                              file_name_reg.get_modified_time(@"E:\a1\b"));

            pause();

            // Try a bigger directory
            Console.WriteLine("Attaching files from " + @"E:\temp\temp");

            string[] file_paths = Directory.GetFiles(@"E:\temp\temp", "*",
                                                     SearchOption.AllDirectories);

            if (file_name_reg.get_path_status(file_paths[0]) == null)
            {
                foreach (string path in file_paths)
                {
                    Console.Write(file_name_reg.add_file(path) + " ");
                }
            }


            Console.WriteLine();
            file_name_reg.print();
            pause();

            // Try reload from disk
            Console.WriteLine("Reload from disk.");
            file_name_reg.Dispose();

            file_name_reg = new FileNameRegistration(file_name_reg_xml);
            file_name_reg.print();

            // Try to create files using explicit file IDs. This is necessary
            // for the restoration process. The file IDs need to be >= 1000
            // per design spec.
            pause();
            file_name_reg.Dispose();

            Console.WriteLine("Adding new files with explicit file IDs\n");
            file_name_reg = new FileNameRegistration(file_name_reg_xml);

            if (file_name_reg.get_path_status(@"c:\test\whatever\123.txt") == null)
            {
                file_name_reg.add_file(@"c:\test\whatever\123.txt", "a", 2123, 0);
                file_name_reg.add_file(@"c:\test\whatever\67.txt", "a", 2067, 0);
                file_name_reg.add_file(@"c:\test\whatever2\320.txt", "a", 2320, 0);
                file_name_reg.set_modified_time(@"c:\test\whatever2\320.txt", DateTime.Now);
                file_name_reg.add_file(@"c:\test\whatever\999.txt", "b", 2999, 0);
            }
            file_name_reg.Dispose();

            file_name_reg = new FileNameRegistration(file_name_reg_xml);

            path_status = file_name_reg.get_path_status(@"c:\test\whatever\999.txt");
            Console.WriteLine(@"c:\test\whatever\999.txt");
            Console.WriteLine("is_file = " + path_status.is_file
                              + "; alt_file_name = " + path_status.alt_file_name
                              + "; modified_time = " + path_status.modified_time);

            file_name_reg.Dispose();
            Console.WriteLine();
        }