Пример #1
0
        private void ImportFile(string fileName)
        {
            // read the file line by line and parse needed data into variables
            #region ReadFile

            StreamReader file = new StreamReader(fileName);

            //create list for each file containing properties that will go to db
            List <LogInfo> LogInfoList = new List <LogInfo>();

            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(identifier) && (line.Contains(".txt") == false && line.Contains(".xls") == false))
                {
                    //create variables to hold parsed data
                    string tempString        = line;
                    string ProcessDate       = tempString.Substring(0, 19);
                    string fileNameStart     = "/";
                    int    fileNameCharacter = tempString.IndexOf(fileNameStart);
                    string SourceFile        = tempString.Substring(fileNameCharacter + 1);
                    string SourceIP          = tempString.Substring(20, 15);
                    string ISBN    = "";
                    string pattern = @"\d{13}?";
                    Regex  rgx     = new Regex(pattern);
                    if (rgx.IsMatch(SourceFile))
                    {
                        Match match = rgx.Match(SourceFile);
                        ISBN = match.Value;
                    }

                    //instantiate loginfo class
                    LogInfo li = new LogInfo();
                    li.ProcessName         = ProcessName;
                    li.ProcessIndex        = ProcessIndex;
                    li.ProcessLogFileName  = ActualFileName;
                    li.PublicationFileName = SourceFile;
                    li.ISBN        = ISBN;
                    li.ProcessDate = ProcessDate;
                    li.SourceIP    = SourceIP;
                    li.Exception   = string.Empty;
                    //add loginfo li to loginfolist
                    LogInfoList.Add(li);

                    //increment line counter
                    lineCounter++;
                }
            }   //end of file reading

            file.Close();
            #endregion

            if (LogInfoList.Count > 0)
            {
                //report to log the number of records being sent to database
                ReportProgress(new ProgressStatus(ProcessName, string.Format("Saving records to database: {0}", LogInfoList.Count)));

                LogInfo saveLogInfo = new LogInfo();
                saveLogInfo.SaveToDatabase(LogInfoList);

                // report archiving to log here
                ReportProgress(new ProgressStatus(ProcessName, string.Format(": Compressing file:{0}...", fileName)));
            }
        }
        private void ImportFile(string fileName)
        {
            //create list for each file containing properties that will go to db
            List <LogInfo> LogInfoList = new List <LogInfo>();

            #region ReadFile

            // read the file line by line and parse needed data into variables
            StreamReader file = new StreamReader(fileName);

            LogInfo li = null;

            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(ProcessingIdentifier))
                {
                    if (li != null)
                    {
                        //add loginfo li to loginfolist
                        LogInfoList.Add(li);
                    }
                    li = new LogInfo();

                    //create variables to hold parsed data
                    string tempString     = line;
                    string ProcessDate    = tempString.Substring(1, 23);
                    int    firstCharacter = tempString.IndexOf(PublisherNameIdentifier);
                    string SourceFile     = tempString.Substring(firstCharacter + 1);
                    string SourceIP       = string.Empty;
                    string ISBN           = "";
                    string pattern        = @"\d{13}?";
                    Regex  rgx            = new Regex(pattern);
                    if (rgx.IsMatch(SourceFile))
                    {
                        Match match = rgx.Match(SourceFile);
                        ISBN = match.Value;
                    }
                    li.ProcessName         = ProcessName;
                    li.ProcessIndex        = ProcessIndex;
                    li.ProcessLogFileName  = ActualFileName;
                    li.PublicationFileName = SourceFile;
                    li.SourceIP            = SourceIP;
                    li.ISBN        = ISBN;
                    li.ProcessDate = ProcessDate;
                    li.Exception   = String.Empty;

                    continue;
                }

                if (li != null && line.Contains(ExceptionIdentifier))
                {
                    int startingCharacter = line.IndexOf(ExceptionIdentifier);
                    Exception = line.Substring(startingCharacter + ExceptionIdentifier.Length);

                    li.Exception = Exception;
                }
            }   //end of file reading

            if (li != null)
            {
                //add loginfo li to loginfolist
                LogInfoList.Add(li);
            }

            file.Close();
            #endregion

            //if the list is populated, send to database
            if (LogInfoList.Count > 0)
            {
                //report to log the number of records being sent to database
                ReportProgress(new ProgressStatus(ProcessName, string.Format("Saving records to database: {0}", LogInfoList.Count)));

                //save list of data elements to database
                LogInfo saveLogInfo = new LogInfo();
                saveLogInfo.SaveToDatabase(LogInfoList);

                // report archiving to log
                ReportProgress(new ProgressStatus(ProcessName, string.Format(": Compressing file:{0}...", fileName)));
            }
        }