Ejemplo n.º 1
0
        public void AddItem(string filename)
        {
            if (System.IO.File.Exists(filename))
            {
                var fileInfo = new FileInfo(filename);

                if (!files.Contains(fileInfo))
                {
                    fileInfo.CheckExtension();
                    if (fileInfo.Extension == null)
                    {
                        return;
                    }

                    Dispatcher.Invoke(() => files.Add(fileInfo));
                }
            }
            else
            {
                foreach (string ss in FilesEnumerator.EnumerateFiles(filename, "*.*", false))
                {
                    AddItem(ss);
                }
            }
        }
Ejemplo n.º 2
0
 private void DisposeFilesEnumerator()
 {
     if (_filesEnumerator != null)
     {
         _filesEnumerator.Dispose();
         _filesEnumerator = null;
     }
 }
Ejemplo n.º 3
0
        private void find_btn_Click(object sender, EventArgs e)
        {
            string searchText    = searchText_textBox.Text.Trim();
            string searchPattern = fileType_comboBox.Text.Trim();
            string path          = openFolder_comboBox.Text.Trim();


            if (_searchState == SearchState.Stopped)
            {
                if (!Directory.Exists(path))
                {
                    MessageBox.Show("Пожалуйста, укажите корректный путь.");
                    this.ActiveControl = openFolder_comboBox;
                    return;
                }

                if (searchText.Length == 0)
                {
                    MessageBox.Show("Пожалуйста, укажите текст для поиска.");
                    this.ActiveControl = searchText_textBox;
                    return;
                }


                DisposeFilesEnumerator();

                try
                {
                    AddLastDirectory(path);

                    _filesEnumerator = new FilesEnumerator(path, searchPattern, allDirectories_checkBox.Checked ?
                                                           SearchOption.AllDirectories :
                                                           SearchOption.TopDirectoryOnly
                                                           );
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

                result_listBox.Items.Clear();
            }


            if (backgroundWorker.IsBusy != true)
            {
                backgroundWorker.RunWorkerAsync(searchText);
            }

            _searchState = SearchState.InProcess;
            ChangeButtons(_searchState);
            toolStripStatusLabel.Text = "Search...";
        }
Ejemplo n.º 4
0
        private async void ButtonBrowse_Click(object sender, RoutedEventArgs e)
        {
            Daramee.Winston.Dialogs.OpenFolderDialog ofd = new Daramee.Winston.Dialogs.OpenFolderDialog();
            ofd.FileName = MeasurePath.Text;
            if (ofd.ShowDialog() == false)
            {
                return;
            }

            (sender as Button).IsEnabled = FilesTree.IsEnabled = false;
            Indicator.Visibility         = Visibility.Visible;

            GCLatencyMode gcOldMode = GCSettings.LatencyMode;

            RuntimeHelpers.PrepareConstrainedRegions();

            try
            {
                GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;

                await Task.Run(() =>
                {
                    var fileEnums = FilesEnumerator.EnumerateFiles(ofd.FileName, "*", false);
                    currentNode   = Node.FilesToNode(fileEnums);

                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        GC.TryStartNoGCRegion(Environment.Is64BitProcess ? (15 * 1024 * 1024) : (256 * 1024 * 1024));

                        FilesTree.ItemsSource  = new Node [] { currentNode };
                        TotalLines.DataContext = currentNode;

                        if (GCSettings.LatencyMode == GCLatencyMode.NoGCRegion)
                        {
                            GC.EndNoGCRegion();
                        }
                    }));
                });
            }
            finally
            {
                GCSettings.LatencyMode = gcOldMode;
            }

            Indicator.Visibility         = Visibility.Hidden;
            (sender as Button).IsEnabled = FilesTree.IsEnabled = true;

            MeasurePath.Text = ofd.FileName;
        }
Ejemplo n.º 5
0
 public void AddItem(string s, bool directoryMode = false)
 {
     if (System.IO.File.Exists(s) || directoryMode)
     {
         var fileInfo = new FileInfo(s);
         if (!FileInfo.Files.Contains(fileInfo))
         {
             FileInfo.Files.Add(fileInfo);
         }
     }
     else
     {
         foreach (string ss in FilesEnumerator.EnumerateFiles(s, "*.*", false))
         {
             AddItem(ss, directoryMode);
         }
     }
 }
 public void ShouldFailIfFolderDoesNotExist()
 {
     try
     {
         var block = FilesEnumerator.GetFilesEnumeratorBlock();
         block.Post(new FilesEnumerator.EnumerateFolderTask
         {
             Folder        = "UnexistingFolder",
             SearchPattern = "*.txt"
         });
         block.EnsureCompleted();
         Assert.Fail();
     }
     catch (AggregateException ex)
     {
         Assert.IsInstanceOfType(ex.GetBaseException(), typeof(DirectoryNotFoundException));
     }
 }
        public void ShouldEnumerateTextFilesInAllSubfolders()
        {
            var block = FilesEnumerator.GetFilesEnumeratorBlock();

            block.Post(new FilesEnumerator.EnumerateFolderTask {
                Folder = "TestFolder", SearchPattern = "*.txt"
            });
            block.Complete();
            var files = Enumerable.Repeat(1, 3).Select(_ => block.Receive(TimeSpan.FromMilliseconds(100))).ToList();

            block.EnsureCompleted();
            Assert.IsTrue(
                files.OrderBy(f => f).SequenceEqual(new[]
            {
                @"TestFolder\Subfolder\TextFile2.txt",
                @"TestFolder\Subfolder\TextFile3.txt",
                @"TestFolder\TextFile1.txt"
            }));
        }
        public async Task ParseFilesAsync(string folderPath, string searchPattern)
        {
            var repository = repositoryFactory();
            await repository.TruncateDataAsync();

            var filesEnumerator = FilesEnumerator.GetFilesEnumeratorBlock();
            var fileCreator     = FileCreator.GetFileCreatorBlock();

            filesEnumerator.LinkToAndPropagateCompleted(fileCreator);

            var fileLinesEnumerator = FileLinesEnumerator.GetFileLinesEnumeratorBlock();

            fileCreator.LinkToAndPropagateCompleted(fileLinesEnumerator);

            var lineSplitter = LineSplitter.GetLineSplitterBlock();

            fileLinesEnumerator.LinkToAndPropagateCompleted(lineSplitter);

            var fileWordCreator = FileWordCreator.GetFileWordCreatorBlock();

            lineSplitter.LinkToAndPropagateCompleted(fileWordCreator);

            var fileWordSaver = FileWordSaver.GetFileWordSaverBlock(repositoryFactory);

            fileWordCreator.LinkToAndPropagateCompleted(fileWordSaver);

            var nullTarget = DataflowBlock.NullTarget <FileWord>();

            fileWordSaver.LinkTo(nullTarget);

            filesEnumerator.Post(new FilesEnumerator.EnumerateFolderTask
            {
                Folder        = folderPath,
                SearchPattern = searchPattern
            });
            filesEnumerator.Complete();
            await fileWordSaver.Completion;
        }
Ejemplo n.º 9
0
        public static async Task <ExcelIndexer> ToIndexer(ExcelIndexerBuilderState state = null)
        {
            IndexedItem [] indexedItems = null;
            DateTime []    indexedDates = null;

            await Task.Run(() =>
            {
                using (LiteDatabase db = new LiteDatabase($"Filename=\"{Path.Combine ( Program.ProgramPath, "ExcelFilesCache.litedb" )}\"; Journal=true; Mode=Shared; Flush=true;"))
                {
                    var cacheFilesCollection  = db.GetCollection <CacheFiles> ("cachefiles");
                    var cachedItemsCollection = db.GetCollection <IndexedItem> ("indexed");

                    var newFile   = new ConcurrentQueue <CacheFiles> ();
                    var newRecord = new ConcurrentQueue <IndexedItem> ();

                    Parallel.ForEach(FilesEnumerator.EnumerateFiles(CustomizedValue.WorkingDirectory, "엑셀자료-*.xlsx", false).AsParallel(), (file) =>
                    {
                        if (file.Contains("일지 양식") || file.Contains("샘플") || Regex.IsMatch(file, todayRegexString))
                        {
                            return;
                        }
                        var fileItem = cacheFilesCollection.FindOne(Query.EQ("Filename", file));
                        if (fileItem != null)
                        {
                            if (fileItem.LastModifiedDateTime.ToString() == File.GetLastWriteTime(file).ToString())
                            {
                                if (state != null)
                                {
                                    Interlocked.Increment(ref state.excelFileCount);
                                    int recordCount = cachedItemsCollection.Find(Query.EQ("Filename", file)).Count();
                                    Interlocked.Add(ref state.totalItemCount, recordCount);
                                    FinderLog.Log($"{Path.GetFileName ( file )}으로부터 {recordCount}개를 이미 인덱싱 했음.");
                                }
                                return;
                            }
                            else
                            {
                                cachedItemsCollection.Delete(Query.EQ("Filename", file));
                                cacheFilesCollection.Delete(Query.EQ("Filename", file));
                                FinderLog.Log($"{Path.GetFileName ( file )}의 인덱싱 데이터가 최신이 아니므로 재인덱싱 시작.");
                            }
                        }
                        else
                        {
                            FinderLog.Log($"{Path.GetFileName ( file )}은 인덱싱되지 않은 파일임.");
                        }

                        try
                        {
                            OleDbConnection connection = new OleDbConnection(
                                $"Provider=\"Microsoft.ACE.OLEDB.12.0\";Data Source=\"{file}\";Extended Properties=\"Excel 12.0;HDR=NO\";"
                                );
                            connection.Open();

                            int count = 0;
                            //Regex.Replace ( connection.GetSchema ( "Tables" ).Rows [ 1 ] [ "TABLE_NAME" ] as string, "['\"]", "" );
                            using (OleDbCommand command = new OleDbCommand(
                                       selectQuery,
                                       connection
                                       ))
                            {
                                using (var reader = command.ExecuteReader())
                                {
                                    if (!reader.Read())
                                    {
                                        state?.OpeningFailedFiles.Enqueue(file);
                                        return;
                                    }

                                    while (reader.Read())
                                    {
                                        object websiteName = reader.GetValue(8);
                                        object url         = reader.GetValue(9);
                                        object baseUrl     = reader.GetValue(31);

                                        if (websiteName == DBNull.Value || url == DBNull.Value || baseUrl == DBNull.Value)
                                        {
                                            break;
                                        }

                                        newRecord.Enqueue(new IndexedItem(websiteName as string, url as string, baseUrl as string, file));

                                        ++count;
                                    }
                                }
                            }

                            connection.Close();

                            if (state != null)
                            {
                                Interlocked.Add(ref state.totalItemCount, count);
                                Interlocked.Increment(ref state.excelFileCount);
                            }

                            newFile.Enqueue(new CacheFiles()
                            {
                                Filename = file, LastModifiedDateTime = File.GetLastWriteTime(file)
                            });

                            FinderLog.Log($"{Path.GetFileName ( file )}의 데이터 {count}개 캐시 완료.");
                        }
                        catch (Exception ex)
                        {
                            FinderLog.Log($"{Path.GetFileName ( file )} 캐시 중 오류 발생: {ex.ToString ()}");
                            state?.OpeningFailedFiles.Enqueue(file);
                        }
                    });

                    cacheFilesCollection.Insert(newFile);
                    cachedItemsCollection.Insert(newRecord);

                    db.Shrink();

                    indexedItems          = new List <IndexedItem> (cachedItemsCollection.FindAll()).ToArray();
                    List <DateTime> dates = new List <DateTime> ();
                    foreach (var f in cacheFilesCollection.FindAll())
                    {
                        string date = "20" + Regex.Match(f.Filename, $"(.*)모니터링일지-[가-힣]+-([0-9][0-9][0-1][0-9][0-3][0-9])\\\\(.*).xlsx").Groups [2].Value;
                        dates.Add(new DateTime(int.Parse(date.Substring(0, 4)), int.Parse(date.Substring(4, 2)), int.Parse(date.Substring(6, 2))));
                    }
                    indexedDates = dates.ToArray();
                }
            });

            GC.Collect();

            return(new ExcelIndexer(indexedItems, indexedDates));
        }