Example #1
0
        private ConfigFull <ConfigAbstract> ReadSerializedConfig(string filePath)
        {
            ConfigFull <ConfigAbstract> config = null;

            if (!(File.Exists(filePath)))
            {
                return(config);
            }

            FileInfo fi = new FileInfo(filePath);

            if (fi.Length == 0)
            {
                return(config);
            }


            BinaryFormatter formatter = new BinaryFormatter();
            string          message   = string.Empty;

            try
            {
                message += $"Try to read '{filePath}':{Environment.NewLine}";
                using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
                {
                    config   = (ConfigFull <ConfigAbstract>)formatter.Deserialize(fs);
                    message += "Success!";
                }
            }
            catch (Exception excpt)
            {
                message += $"{excpt.Message}:{Environment.NewLine}{excpt.ToString()}";
            }

            EvntInfoMessage?.Invoke(this, new TextEventArgs(message));
            EvntCollectionFull?.Invoke(this, new BoolEventArgs(true));//collection is full

            return(config);
        }
Example #2
0
        public async Task <IList <string> > ReadAsync(string filePath, Encoding encoding, int maxElementsInDictionary)
        {
            Text = new List <string>(maxElementsInDictionary);

            const int         DefaultBufferSize = 4096;
            const FileOptions DefaultOptions    = FileOptions.Asynchronous | FileOptions.SequentialScan;

            string currentRow;

            importedRows = 0;

            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
            {
                using (var reader = new StreamReader(stream, encoding))
                {
                    while ((currentRow = await reader.ReadLineAsync())?.Trim()?.Length > 1)
                    {
                        Text.Add(currentRow);
                        importedRows++;

                        if (importedRows > 0 && importedRows % maxElementsInDictionary == 0)
                        {
                            EvntCollectionFull?.Invoke(this, new BoolEventArgs(true));//collection is full
                            await Task.Delay(TimeSpan.FromSeconds(0.2));

                            Text = new List <string>(maxElementsInDictionary);
                        }
                    }
                }
            }

            if (Text?.Count > 0)
            {
                EvntCollectionFull?.Invoke(this, new BoolEventArgs(true));//last part of the collection

                await Task.Delay(TimeSpan.FromSeconds(0.2));
            }
            return(Text);
        }
Example #3
0
        async Task ImportTextFile(string filePath, Encoding encoding, int maxElementsInDictionary)
        {
            const int         DefaultBufferSize = 4096;
            const FileOptions DefaultOptions    = FileOptions.Asynchronous | FileOptions.SequentialScan;
            string            nameColumns       = null;
            string            currentRow;
            IModels           models;

            columnNames      = new ModelCommonStore();
            listCommonModels = new List <IModels>(maxElementsInDictionary);

            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
            {
                using (var reader = new StreamReader(stream, encoding))
                {
                    while ((currentRow = await reader.ReadLineAsync())?.Trim()?.Length > 10)
                    {
                        if (nameColumns == null)
                        {
                            importedRows = 0;

                            parsedModel = new ParserRowModelCommon(currentRow);
                            columnNames = parsedModel.MatchColumnToAlias();
                            nameColumns = parsedModel.ImportedColumnName;

                            EvntHeaderReady?.Invoke(this, new BoolEventArgs(true));//cHeader is ready
                        } //first found not_empty_line containes name columns
                        else
                        {
                            parsedModel = new ParserRowModelCommon(currentRow, nameColumns);

                            models = parsedModel?.ConvertRowToModels();

                            if (models?.list?.Count > 0)
                            {
                                importedRows++;
                                listCommonModels.Add(models);

                                if (importedRows > 0 && importedRows % maxElementsInDictionary == 0)
                                {
                                    EvntCollectionFull?.Invoke(this, new BoolEventArgs(true));//collection is full
                                    EvntInfoMessage?.Invoke(this, new TextEventArgs($"lastRow: {currentRow}" +
                                                                                    $"{Environment.NewLine}parsed: {models.ToString()}" +
                                                                                    $"{Environment.NewLine}Ожидаю пока запишутся данные(до 5 сек.)..."));

                                    FileReaderModels.evntWaitHandle.WaitOne(5000);

                                    listCommonModels = new List <IModels>(maxElementsInDictionary);
                                }
                            }
                        }
                    }
                }
            }

            if (listCommonModels?.Count > 0)
            {
                EvntCollectionFull?.Invoke(this, new BoolEventArgs(true));//last part of the collection
                EvntInfoMessage?.Invoke(this, new TextEventArgs($"Ожидаю пока запишется последняя часть данных(до 2 сек.)..."));
                FileReaderModels.evntWaitHandle.WaitOne(2000);
            }
        }
Example #4
0
        private void ImportExcelFile(string filePath, int maxElementsInDictionary)
        {
            string  nameColumns = null;
            string  currentRow;
            IModels models;

            columnNames      = new ModelCommonStore();
            listCommonModels = new List <IModels>(maxElementsInDictionary);
            var fi = new FileInfo(filePath);

            using (var package = new ExcelPackage(fi))
            {
                var workbook  = package.Workbook;
                var worksheet = workbook.Worksheets.First();

                //get the first worksheet in the workbook
                int colCount = worksheet.Dimension.End.Column;  //get Column Count
                int rowCount = worksheet.Dimension.End.Row;     //get row count

                EvntInfoMessage?.Invoke(this, new TextEventArgs($"File contains: {colCount} columns{Environment.NewLine}and {rowCount} rows"));
                for (int row = 1; row <= rowCount; row++)
                {
                    currentRow = string.Empty;
                    for (int col = 1; col <= colCount; col++)
                    {
                        currentRow += $"{worksheet.Cells[row, col]?.Value?.ToString()?.Trim()}|";
                    }

                    currentRow = currentRow?.TrimEnd('|');

                    if (!currentRow.Contains("|"))
                    {
                        continue;
                    }
                    if (nameColumns == null)
                    {
                        importedRows = 0;

                        parsedModel = new ParserRowModelCommon(currentRow);
                        columnNames = parsedModel.MatchColumnToAlias();
                        nameColumns = parsedModel.ImportedColumnName;

                        EvntHeaderReady?.Invoke(this, new BoolEventArgs(true));//cHeader is ready
                    } //first found not_empty_line will be contained name columns
                    else
                    {
                        parsedModel = new ParserRowModelCommon(currentRow, nameColumns);

                        models = parsedModel?.ConvertRowToModels();

                        if (!(models?.list?.Count > 0))
                        {
                            continue;
                        }
                        importedRows++;
                        listCommonModels.Add(models);

                        if (importedRows <= 0 || importedRows % maxElementsInDictionary != 0)
                        {
                            continue;
                        }
                        EvntCollectionFull?.Invoke(this, new BoolEventArgs(true));//collection is full
                        EvntInfoMessage?.Invoke(this, new TextEventArgs($"lastRow: {currentRow}" +
                                                                        $"{Environment.NewLine}parsed: {models.ToString()}" +
                                                                        $"{Environment.NewLine}Ожидаю пока данные запишутся (до 5 сек.)..."));

                        FileReaderModels.evntWaitHandle.WaitOne(5000);
                        listCommonModels = new List <IModels>(maxElementsInDictionary);
                    }
                }
            }

            if (!(listCommonModels?.Count > 0))
            {
                return;
            }
            EvntCollectionFull?.Invoke(this, new BoolEventArgs(true));//last part of the collection
            EvntInfoMessage?.Invoke(this, new TextEventArgs($"Ожидаю пока запишется последняя часть данных (до 5 сек.)..."));
            FileReaderModels.evntWaitHandle.WaitOne(5000);
        }