Ejemplo n.º 1
0
        private void StartProcessing(SnapSettings settings)
        {
            // ensure source path format
            settings.rootFolder = System.IO.Path.GetFullPath(settings.rootFolder);
            if (settings.rootFolder.EndsWith(@"\"))
            {
                settings.rootFolder = settings.rootFolder.Substring(0, settings.rootFolder.Length - 1);
            }
            if (Utils.IsWildcardMatch("?:", settings.rootFolder, false))
            {
                settings.rootFolder += @"\";                                                                            // add backslash to path if only letter and colon eg "c:"
            }
            // add slash or backslash to end of link (in cases where it is clear that we we can)
            if (settings.linkFiles)
            {
                if (!settings.linkRoot.EndsWith(@"/") && settings.linkRoot.ToLower().StartsWith(@"http"))                       // web site
                {
                    settings.linkRoot += @"/";
                }
                if (!settings.linkRoot.EndsWith(@"\") && Utils.IsWildcardMatch("?:*", settings.linkRoot, false))                        // local disk
                {
                    settings.linkRoot += @"\";
                }
            }

            Cursor.Current      = Cursors.WaitCursor;
            this.Text           = "Snap2HTML (Working... Press Escape to Cancel)";
            tabControl1.Enabled = false;
            backgroundWorker.RunWorkerAsync(argument: settings);
        }
Ejemplo n.º 2
0
        private void cmdCreate_Click(object sender, EventArgs e)
        {
            // ask for output file
            string fileName = new System.IO.DirectoryInfo(txtRoot.Text + @"\").Name;

            char[] invalid = System.IO.Path.GetInvalidFileNameChars();
            for (int i = 0; i < invalid.Length; i++)
            {
                fileName = fileName.Replace(invalid[i].ToString(), "");
            }

            saveFileDialog1.DefaultExt = "html";
            if (!fileName.ToLower().EndsWith(".html"))
            {
                fileName += ".html";
            }
            saveFileDialog1.FileName         = fileName;
            saveFileDialog1.Filter           = "HTML files (*.html)|*.html|All files (*.*)|*.*";
            saveFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(txtRoot.Text);
            saveFileDialog1.CheckPathExists  = true;
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (!saveFileDialog1.FileName.ToLower().EndsWith(".html"))
            {
                saveFileDialog1.FileName += ".html";
            }

            // begin generating html
            var settings = new SnapSettings()
            {
                rootFolder      = txtRoot.Text,
                title           = txtTitle.Text,
                outputFile      = saveFileDialog1.FileName,
                skipHiddenItems = !chkHidden.Checked,
                skipSystemItems = !chkSystem.Checked,
                openInBrowser   = chkOpenOutput.Checked,
                linkFiles       = chkLinkFiles.Checked,
                linkRoot        = txtLinkRoot.Text,
            };

            StartProcessing(settings);
        }
Ejemplo n.º 3
0
        // --- Helper functions (must be static to avoid thread problems) ---

        private static List <SnappedFolder> GetContent(SnapSettings settings, BackgroundWorker bgWorker)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var result = new List <SnappedFolder>();

            // Get all folders
            var dirs = new List <string>();

            dirs.Insert(0, settings.rootFolder);
            DirSearch(settings.rootFolder, dirs, settings.skipHiddenItems, settings.skipSystemItems, stopwatch, bgWorker);
            dirs = Utils.SortDirList(dirs);

            if (bgWorker.CancellationPending)
            {
                return(null);
            }

            var totFiles = 0;

            stopwatch.Restart();

            try
            {
                string modified_date;
                string created_date;

                // Parse each folder
                for (int d = 0; d < dirs.Count; d++)
                {
                    // Get folder properties
                    var dirName    = dirs[d];
                    var currentDir = new SnappedFolder(Path.GetFileName(dirName), Path.GetDirectoryName(dirName));
                    if (dirName == Path.GetPathRoot(dirName))
                    {
                        currentDir = new SnappedFolder("", dirName);
                    }

                    modified_date = "";
                    created_date  = "";
                    try
                    {
                        modified_date = Utils.ToUnixTimestamp(System.IO.Directory.GetLastWriteTime(dirName).ToLocalTime()).ToString();
                        created_date  = Utils.ToUnixTimestamp(System.IO.Directory.GetCreationTime(dirName).ToLocalTime()).ToString();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("{0} Exception caught.", ex);
                    }
                    currentDir.Properties.Add("Modified", modified_date);
                    currentDir.Properties.Add("Created", created_date);

                    // Get files in folder
                    List <string> files;
                    try
                    {
                        files = new List <string>(System.IO.Directory.GetFiles(dirName, "*.*", System.IO.SearchOption.TopDirectoryOnly));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("{0} Exception caught.", ex);
                        result.Add(currentDir);
                        continue;
                    }
                    files.Sort();

                    // Get file properties
                    foreach (string sFile in files)
                    {
                        totFiles++;
                        if (stopwatch.ElapsedMilliseconds >= 50)
                        {
                            bgWorker.ReportProgress(0, "Reading files... " + totFiles + " (" + sFile + ")");
                            stopwatch.Restart();
                        }

                        if (bgWorker.CancellationPending)
                        {
                            return(null);
                        }

                        var currentFile = new SnappedFile(Path.GetFileName(sFile));
                        try
                        {
                            System.IO.FileInfo fi = new System.IO.FileInfo(sFile);
                            var isHidden          = (fi.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden;
                            var isSystem          = (fi.Attributes & System.IO.FileAttributes.System) == System.IO.FileAttributes.System;

                            if ((isHidden && settings.skipHiddenItems) || (isSystem && settings.skipSystemItems))
                            {
                                continue;
                            }

                            currentFile.Properties.Add("Size", fi.Length.ToString());

                            modified_date = "-";
                            created_date  = "-";
                            try
                            {
                                modified_date = Utils.ToUnixTimestamp(fi.LastWriteTime.ToLocalTime()).ToString();
                                created_date  = Utils.ToUnixTimestamp(fi.CreationTime.ToLocalTime()).ToString();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("{0} Exception caught.", ex);
                            }

                            currentFile.Properties.Add("Modified", modified_date);
                            currentFile.Properties.Add("Created", created_date);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("{0} Exception caught.", ex);
                        }

                        currentDir.Files.Add(currentFile);
                    }

                    result.Add(currentDir);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("{0} exception caught: {1}", ex, ex.Message);
            }

            return(result);
        }
Ejemplo n.º 4
0
        private void frmMain_Shown(object sender, EventArgs e)
        {
            // parse command line
            var commandLine      = Environment.CommandLine;
            var splitCommandLine = Arguments.SplitCommandLine(commandLine);
            var arguments        = new Arguments(splitCommandLine);

            // first test for single argument (ie path only)
            if (splitCommandLine.Length == 2 && !arguments.Exists("path"))
            {
                if (System.IO.Directory.Exists(splitCommandLine[1]))
                {
                    SetRootPath(splitCommandLine[1]);
                }
            }

            var settings = new SnapSettings();

            if (arguments.Exists("path") && arguments.Exists("outfile"))
            {
                this.runningAutomated = true;

                settings.rootFolder = arguments.Single("path");
                settings.outputFile = arguments.Single("outfile");

                // First validate paths
                if (!System.IO.Directory.Exists(settings.rootFolder))
                {
                    if (!arguments.Exists("silent"))
                    {
                        MessageBox.Show("Input path does not exist: " + settings.rootFolder, "Automation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    Application.Exit();
                }
                if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(settings.outputFile)))
                {
                    if (!arguments.Exists("silent"))
                    {
                        MessageBox.Show("Output path does not exist: " + System.IO.Path.GetDirectoryName(settings.outputFile), "Automation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    Application.Exit();
                }

                // Rest of settings

                settings.skipHiddenItems = !arguments.Exists("hidden");
                settings.skipSystemItems = !arguments.Exists("system");
                settings.openInBrowser   = false;

                settings.linkFiles = false;
                if (arguments.Exists("link"))
                {
                    settings.linkFiles = true;
                    settings.linkRoot  = arguments.Single("link");
                }

                settings.title = "Snapshot of " + settings.rootFolder;
                if (arguments.Exists("title"))
                {
                    settings.title = arguments.Single("title");
                }
            }

            // keep window hidden in silent mode
            if (arguments.IsTrue("silent") && this.runningAutomated)
            {
                Visible = false;
            }
            else
            {
                Opacity = 100;
            }

            if (this.runningAutomated)
            {
                StartProcessing(settings);
            }
        }