Exemplo n.º 1
0
        private void Button1_Click(object sender, EventArgs e)
        {
            const Int32 BufferSize = 128;
            String      line;

            PCSDat pcs = new PCSDat();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var sFile = openFileDialog1.FileName;

                    using (var fileStream = File.OpenRead(sFile))

                        pcs.GetProjectDataFromFile(sFile);
                }
                catch (SecurityException ex)
                {
                    MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
                                    $"Details:\n\n{ex.StackTrace}");
                }
            }
        }
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker.ReportProgress(0, "Reading folders...");
            var sbDirArrays = new StringBuilder();
            int prevDepth   = -100;

            StringBuilder strCustomerArray = new StringBuilder();
            PCSDat        pcs = new PCSDat();

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

            dirs.Insert(0, txtRoot.Text);

            var skipHidden = (chkHidden.CheckState == CheckState.Unchecked);
            var skipSystem = (chkSystem.CheckState == CheckState.Unchecked);

            DirSearch(txtRoot.Text, dirs, skipHidden, skipSystem);

            dirs = SortDirList(dirs);

            int totDirs = 0;

            dirs.Add("*EOF*");
            long totSize         = 0;
            int  totFiles        = 0;
            var  lineBreakSymbol = "\n";                // could set to \n to make html more readable at the expense of increased size

            // Get files in folders
            for (int d = 0; d < dirs.Count; d++)
            {
                string currentDir = dirs[d];

                try
                {
                    int newDepth = currentDir.Split(System.IO.Path.DirectorySeparatorChar).Length;
                    if (currentDir.Length < 64 && currentDir == System.IO.Path.GetPathRoot(currentDir))
                    {
                        newDepth--;                                                                                                             // fix reading from rootfolder, <64 to avoid going over MAX_PATH
                    }
                    prevDepth = newDepth;

                    var sbCurrentDirArrays = new StringBuilder();

                    if (currentDir != "*EOF*")
                    {
                        bool no_problem = true;

                        try
                        {
                            var files = new List <string>(System.IO.Directory.GetFiles(currentDir, "*.*", System.IO.SearchOption.TopDirectoryOnly));
                            //var files = new List<string>(System.IO.Directory.GetFiles(currentDir, "*.*", System.IO.SearchOption.TopDirectoryOnly) .Where(s => !s.Contains("~")) );

                            files.Sort();
                            int f = 0;

                            string last_write_date = "-";
                            last_write_date = System.IO.Directory.GetLastWriteTime(currentDir).ToLocalTime().ToString();
                            long dir_size = 0;

                            sbCurrentDirArrays.Append("D.p([" + lineBreakSymbol);

                            var sDirWithForwardSlash = currentDir.Replace(@"\", "/");

                            sbCurrentDirArrays.Append("\"").Append(MakeCleanJsString(sDirWithForwardSlash)).Append("|").Append(dir_size).Append("|").Append(last_write_date).Append("\"," + lineBreakSymbol);

                            f++;

                            long dirSize = 0;

                            pcs = new PCSDat();

                            foreach (string sFile in files)
                            {
                                bool bInclude  = true;
                                long fi_length = 0;
                                last_write_date = "-";

                                try
                                {
                                    System.IO.FileInfo fi = new System.IO.FileInfo(sFile);

                                    if ((fi.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden && chkHidden.CheckState != CheckState.Checked)
                                    {
                                        bInclude = false;
                                    }
                                    if ((fi.Attributes & System.IO.FileAttributes.System) == System.IO.FileAttributes.System && chkSystem.CheckState != CheckState.Checked)
                                    {
                                        bInclude = false;
                                    }

                                    fi_length = fi.Length;

                                    try
                                    {
                                        last_write_date = fi.LastWriteTime.ToLocalTime().ToString();

                                        string ext = fi.Extension;

                                        pcs.GetProjectDataFromFile(sFile);  // tikrinam, ar tai PCS failas; jai taip, - istraukiam info apie projekta

                                        if (pcs.IsPcsFile == true)
                                        {
                                            string[] custList = pcs.Customer.Split(';');

                                            foreach (string custname in custList)
                                            {
                                                if (!strCustomerArray.ToString().Contains(custname))
                                                {
                                                    if (strCustomerArray.Length > 0)
                                                    {
                                                        strCustomerArray.Append("|");
                                                    }

                                                    strCustomerArray.Append(custname);
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine("{0} Exception caught.", ex);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("{0} Exception caught.", ex);
                                    bInclude = false;
                                }

                                if (bInclude)
                                {
                                    sbCurrentDirArrays.Append("\"").Append(MakeCleanJsString(System.IO.Path.GetFileName(sFile))).Append("|").Append(fi_length).Append("|").Append(last_write_date).Append("|").Append(pcs.ToString()).Append("\"," + lineBreakSymbol);
                                    totSize += fi_length;
                                    dirSize += fi_length;
                                    totFiles++;
                                    f++;

                                    pcs.ClearProjData();

                                    //if( totFiles % 9 == 0 )
                                    //{
                                    backgroundWorker.ReportProgress(0, "Reading files... " + totFiles + " (" + sFile + ")");
                                    //}
                                }
                                if (backgroundWorker.CancellationPending)
                                {
                                    backgroundWorker.ReportProgress(0, "Operation Cancelled!");
                                    return;
                                }
                            }

                            // Add total dir size
                            sbCurrentDirArrays.Append("").Append(dirSize).Append(",");

                            // Add subfolders
                            string subdirs = "";

                            List <string> lstSubDirs = new List <string>(System.IO.Directory.GetDirectories(currentDir));

                            lstSubDirs = SortDirList(lstSubDirs);

                            foreach (string sTmp in lstSubDirs)
                            {
                                int i = dirs.IndexOf(sTmp);
                                if (i != -1)
                                {
                                    subdirs += i + "|";
                                }
                            }

                            if (subdirs.EndsWith("|"))
                            {
                                subdirs = subdirs.Remove(subdirs.Length - 1);
                            }

                            sbCurrentDirArrays.Append("\"").Append(subdirs).Append("\"" + lineBreakSymbol);                                     // subdirs
                            sbCurrentDirArrays.Append("])");
                            sbCurrentDirArrays.Append("\n");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("{0} Exception caught.", ex);
                            no_problem = false;
                        }

                        if (no_problem == false)                                // We need to keep folder even if error occurred for integrity
                        {
                            var sDirWithForwardSlash = currentDir.Replace(@"\", "/");

                            sbCurrentDirArrays = new StringBuilder();

                            sbCurrentDirArrays.Append("D.p([\"").Append(MakeCleanJsString(sDirWithForwardSlash)).Append("*0*-\"," + lineBreakSymbol);
                            sbCurrentDirArrays.Append("0," + lineBreakSymbol);                                  // total dir size
                            sbCurrentDirArrays.Append("\"\"" + lineBreakSymbol);                                // subdirs
                            sbCurrentDirArrays.Append("])" + lineBreakSymbol);
                            no_problem = true;
                        }

                        if (no_problem)
                        {
                            sbDirArrays.Append(sbCurrentDirArrays.ToString());
                            totDirs++;
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine("{0} exception caught: {1}", ex, ex.Message);
                }
            }


            if (backgroundWorker.CancellationPending)
            {
                backgroundWorker.ReportProgress(0, "User cancelled");
                return;
            }

            // -- Generate Output --
            backgroundWorker.ReportProgress(0, "Generating HTML file...");

            // Read template
            var sbContent = new StringBuilder();

            try
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + System.IO.Path.DirectorySeparatorChar + "template.html"))
                {
                    sbContent.Append(reader.ReadToEnd());
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to open 'Template.html' for reading...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                backgroundWorker.ReportProgress(0, "An error occurred...");
                return;
            }

            // Build HTML
            sbContent.Replace("[DIR DATA]", sbDirArrays.ToString());
            sbContent.Replace("[TITLE]", txtTitle.Text);
            sbContent.Replace("[APP LINK]", "http://www.rlvision.com");
            sbContent.Replace("[APP NAME]", Application.ProductName);
            sbContent.Replace("[APP VER]", Application.ProductVersion.Split('.')[0] + "." + Application.ProductVersion.Split('.')[1]);
            sbContent.Replace("[GEN TIME]", DateTime.Now.ToString("t"));
            sbContent.Replace("[GEN DATE]", DateTime.Now.ToString("d"));
            sbContent.Replace("[NUM FILES]", totFiles.ToString());
            sbContent.Replace("[NUM DIRS]", totDirs.ToString());
            sbContent.Replace("[TOT SIZE]", totSize.ToString());
            if (chkLinkFiles.Checked)
            {
                sbContent.Replace("[LINK FILES]", "true");
                sbContent.Replace("[LINK ROOT]", txtLinkRoot.Text.Replace(@"\", "/"));
                sbContent.Replace("[SOURCE ROOT]", txtRoot.Text.Replace(@"\", "/"));

                string link_root = txtLinkRoot.Text.Replace(@"\", "/");
                if (IsWildcardMatch(@"?:/*", link_root, false))                     // "file://" is needed in the browser if path begins with drive letter, else it should not be used
                {
                    sbContent.Replace("[LINK PROTOCOL]", @"file://");
                }
                else
                {
                    sbContent.Replace("[LINK PROTOCOL]", "");
                }
            }
            else
            {
                sbContent.Replace("[LINK FILES]", "false");
                sbContent.Replace("[LINK PROTOCOL]", "");
                sbContent.Replace("[LINK ROOT]", "");
                sbContent.Replace("[SOURCE ROOT]", txtRoot.Text.Replace(@"\", "/"));
            }

            sbContent.Replace("[PCS_CUSTOMERS]", "CUST.p([\"" + strCustomerArray.ToString() + "\"])\n");



            // Write output file
            try
            {
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(saveFileDialog1.FileName))
                {
                    writer.Write(sbContent.ToString());
                }

                if (chkOpenOutput.Checked == true)
                {
                    System.Diagnostics.Process.Start(saveFileDialog1.FileName);
                }
            }
            catch (System.Exception excpt)
            {
                MessageBox.Show("Failed to open file for writing:\n\n" + excpt, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                backgroundWorker.ReportProgress(0, "An error occurred...");
                return;
            }

            // Ready!
            Cursor.Current = Cursors.Default;
            backgroundWorker.ReportProgress(100, "Ready!");
        }