示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItemExportText_Click(object sender, EventArgs e)
        {
            var listView = listViewSearchResult;

            foreach (ListViewItem item in listView.SelectedItems)
            {
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    var filePath = item.Tag as string;
                    sfd.FileName         = Path.GetFileNameWithoutExtension(filePath) + ".txt";
                    sfd.InitialDirectory = Path.GetDirectoryName(filePath);
                    sfd.Filter           = "Text File (*.txt)|*.txt".Localize();

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        string extractedText = string.Empty;
                        XDoc2TxtManager.Extract(filePath, ref extractedText);
                        try
                        {
                            File.WriteAllText(sfd.FileName, extractedText);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(
                                "Failed to save the file".Localize(), "Error".Localize(),
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation
                                );
                        }
                    }
                }
                break;
            }
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listViewSearchResult_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var listView = listViewSearchResult;

            foreach (ListViewItem item in listView.SelectedItems)
            {
                using (var previewWindow = new FormTextPreview())
                {
                    var    filePath      = item.Tag as string;
                    string extractedText = string.Empty;
                    if (XDoc2TxtManager.Extract(filePath, ref extractedText) > 0)
                    {
                        previewWindow.SetText(extractedText, SearchedText);
                        previewWindow.ShowDialog();
                    }
                }
                break;
            }
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorkerSearch_DoWork(object sender, DoWorkEventArgs e)
        {
            var args       = e.Argument as List <string>;
            var targetPath = args[0];
            var extensions = args[1];
            var searchText = args[2];

            var files = new List <string>();

            if (Directory.Exists(targetPath))
            {
                var caches = _caches.HasCache(targetPath);

                if (caches == null)
                {
                    foreach (var extension in extensions.Split())
                    {
                        var param = new WorkerProgressParam();
                        foreach (var file in Directory.EnumerateFiles(targetPath, extension, SearchOption.AllDirectories))
                        {
                            files.Add(file);
                            if (backgroundWorkerSearch.CancellationPending)
                            {
                                param.Mode = WorkerProgressParam.WorkerMode.ModeCanceled;
                                backgroundWorkerSearch.ReportProgress(0, param);
                                break;
                            }

                            param.Mode      = WorkerProgressParam.WorkerMode.ModeSearchingFiles;
                            param.FileCount = files.Count;
                            backgroundWorkerSearch.ReportProgress(0, param);
                        }
                    }
                }
                else
                {
                    files.AddRange(caches);
                }

                _caches.UpdateCache(targetPath, files);
            }
            else
            {
                files.Add(targetPath);
            }

            int count       = 0;
            var resultParam = new WorkerResultParam();

            resultParam.FileCount  = files.Count;
            resultParam.SearchText = searchText;
            foreach (var file in files)
            {
                var param = new WorkerProgressParam();
                param.Mode      = WorkerProgressParam.WorkerMode.ModeCheckFile;
                param.FileCount = ++count;
                param.FilePath  = file;
                backgroundWorkerSearch.ReportProgress((int)(((double)count / (double)files.Count) * 100.0), param);

                string extractedText = string.Empty;
                try
                {
                    XDoc2TxtManager.Extract(file, ref extractedText);
                }
                catch (Exception)
                {
                    resultParam.Error = WorkerResultParam.Errors.LibraryDoesNotWork;
                    break;
                }


                int       index            = 0;
                const int aroundTextLength = 20;
                while ((index = extractedText.IndexOf(searchText, index)) != -1 && !backgroundWorkerSearch.CancellationPending)
                {
                    var result     = new WorkerResultParam.Result();
                    int startIndex = Math.Max(0, index - aroundTextLength);
                    result.SearchedAroundText = extractedText.Substring(startIndex, aroundTextLength * 2 + searchText.Length);
                    result.FilePath           = file;
                    resultParam.Results.Add(result);

                    index = index + 1;
                }

                if (backgroundWorkerSearch.CancellationPending)
                {
                    param.Mode = WorkerProgressParam.WorkerMode.ModeCanceled;
                    backgroundWorkerSearch.ReportProgress(0, param);
                    break;
                }
            }
            e.Result = resultParam;
        }