コード例 #1
0
        void GetText(SRCResult item)
        {
            try
            {
                string ext    = Path.GetExtension(Path.GetExtension(item.FileName)).ToLower();
                string result = "";

                if (File.Exists(item.FileName))
                {
                    if (chdatafromDB.Checked)
                    {
                        if (item.FileHash != TMD5.ComputeFilesMD5(item.FileName))
                        {
                            if (MessageBox.Show("Данные в файле отличаются от сохраненной копии, загрузить данные из файла?", "Найдено измененное содержимое файла", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                result = File.ReadAllText(item.FileName);
                            }
                        }

                        if (result == "")
                        {
                            SQLiteCommand cmd = new SQLiteCommand();
                            cmd.Connection  = sqlcon;
                            cmd.CommandText = $"SELECT content  FROM {_FTS_TABLE} where {_FTS_TABLE}.fileid={item.FileID};";
                            SQLiteDataReader r = cmd.ExecuteReader();
                            while (r.Read())
                            {
                                result = r[0].ToString();
                            }
                        }
                    }
                    else
                    {
                        result = File.ReadAllText(item.FileName);
                    }
                }
                else
                {
                    Invoke((MethodInvoker) delegate
                    {
                        rlog.Visible = true;
                        rlog.AppendText($"File not found {item.FileName}" + Environment.NewLine);
                        rlog.Height = 55;
                    });
                }
                UPDFNDText(result, ext);
            }
            catch (Exception ex)
            {
                Invoke((MethodInvoker) delegate
                {
                    rlog.Visible = true;
                    rlog.AppendText("SrcFiles." + ex.Message + Environment.NewLine);
                    rlog.Height = 55;
                });
            }

            Invoke((MethodInvoker) delegate
            {
                try
                {
                    _srcw_curr_pos = 0;
                    var ll         = _tb.SelectNext(txtText4SEarch.Text, false, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    _srcw          = _tb.FindLines(txtText4SEarch.Text, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    selword(_srcw_curr_pos);
                }
                catch (Exception ex)
                {
                    Invoke((MethodInvoker) delegate
                    {
                        rlog.Visible = true;
                        rlog.AppendText("SelectNext." + ex.Message + Environment.NewLine);
                        rlog.Height = 55;
                    });
                }
            });
        }
コード例 #2
0
        void HardFileHashRecalc()
        {
            SetStatus("Recalc hash of files in progress");
            string qry = $" select FileID ,FileName ,FileHash from { _FTS_TABLE}_Files";

            try
            {
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.Connection = sqlcon;

                cmd.CommandText = qry;
                SQLiteDataReader r = cmd.ExecuteReader();
                while (r.Read())
                {
                    SRCResult f = new SRCResult();
                    f.FileID   = Convert.ToInt32(r["FileID"]);
                    f.FileName = r["FileName"].ToString();
                    f.FileHash = r["FileHash"].ToString();
                    _FoundFiles.Add(f);
                }

                int i = 0;
                SetPBVisible(true);
                SetPBMax(_FoundFiles.Count);
                foreach (var fl in _FoundFiles)
                {
                    try
                    {
                        if (File.Exists(fl.FileName))
                        {
                            string filehash = TMD5.ComputeFilesMD5(fl.FileName);
                            qry = $"update {_FTS_TABLE}_Files set FileHash='{filehash}' where FileID={fl.FileID}";
                            sql3runquery(qry, ref sqlcon);
                        }
                        UPDPB1(i);
                        i++;
                    }
                    catch (Exception ex)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            rlog.Visible = true;
                            rlog.AppendText("HardFileHashRecalc." + ex.Message + Environment.NewLine);
                            rlog.Height = 55;
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Invoke((MethodInvoker) delegate
                {
                    rlog.Visible = true;
                    rlog.AppendText("HardFileHashRecalc." + ex.Message + Environment.NewLine);
                    rlog.Height = 55;
                });
            }
            UPDPB1(0);
            SetPBVisible(false);
            SetStatus("Ready for work");
        }
コード例 #3
0
        private void add2index(bool updcontent = false)
        {
            SetStatus("Scanning for files");
            var files = Directory.GetFiles(txtpath4src.Text, txtmask.Text, System.IO.SearchOption.AllDirectories);

            if (files.Length == 0)
            {
                return;
            }

            SetPBVisible(true);
            SetPBMax(files.Length);

            SetStatus("Scan complite");
            string qry = "";
            int    i   = 0;

            foreach (string fname in files)
            {
                SetStatus($"Add '{fname}' to index.");
                if (File.Exists(fname))
                {
                    try
                    {
                        SRCResult FndData  = _FilesInDB.FirstOrDefault(x => x.FileName == fname);
                        int       rowid    = FndData != null ? FndData.FileID : -1;
                        string    FileHash = TMD5.ComputeFilesMD5(fname);
                        string    runqry   = "";
                        if ((updcontent && FileHash != FndData?.FileHash) || rowid == -1)
                        {
                            using (SQLiteTransaction myTransaction = sqlcon.BeginTransaction())
                            {
                                using (SQLiteCommand servCommand = new SQLiteCommand(sqlcon))
                                {
                                    SQLiteParameter FileName = new SQLiteParameter();
                                    if (rowid != -1)
                                    {
                                        sql3runquery($"delete from {_FTS_TABLE} where fileid={rowid}", ref sqlcon);
                                        sql3runquery($"delete from {_FTS_TABLE}_Files where fileid={rowid}", ref sqlcon);
                                    }

                                    servCommand.CommandText = $"INSERT INTO {_FTS_TABLE}_Files (FileName) VALUES (?)";
                                    FileName.Value          = fname;
                                    servCommand.Parameters.Add(FileName);
                                    var result = servCommand.ExecuteNonQueryAsync();
                                    if (result.IsFaulted)
                                    {
                                        Invoke((MethodInvoker) delegate
                                        {
                                            rlog.Visible = true;
                                            rlog.AppendText($"InsertFileName fault:{result.Exception.InnerException.Message}");
                                            rlog.Height = 55;
                                        });
                                        break;
                                    }

                                    long rid      = servCommand.Connection.LastInsertRowId;
                                    int  newrowid = GetRowID(fname);

                                    SQLiteCommand insCommand = new SQLiteCommand(sqlcon);
                                    insCommand.CommandText = $"INSERT OR REPLACE INTO {_FTS_TABLE} (fileid, content) VALUES(?, ?)";

                                    SQLiteParameter FileData = new SQLiteParameter();
                                    SQLiteParameter FileID   = new SQLiteParameter();
                                    FileData.Value = File.ReadAllText(fname);
                                    FileID.Value   = newrowid;
                                    insCommand.Parameters.Add(FileID);
                                    insCommand.Parameters.Add(FileData);

                                    result = insCommand.ExecuteNonQueryAsync();
                                    if (result.IsFaulted)
                                    {
                                        Invoke((MethodInvoker) delegate
                                        {
                                            rlog.Visible = true;
                                            rlog.AppendText($"InsertFileName fault:{result.Exception.InnerException.Message}");
                                            rlog.Height = 55;
                                        });
                                        break;
                                    }

                                    servCommand.CommandText = $"Update {_FTS_TABLE}_Files set FileHash='(?)' where FileID=(?)";
                                    servCommand.Parameters.Add(new SQLiteParameter("FileHash", FileHash));
                                    servCommand.Parameters.Add(new SQLiteParameter("FileID", rowid));
                                    var result3 = servCommand.ExecuteNonQueryAsync();
                                    if (result3.IsFaulted)
                                    {
                                        Invoke((MethodInvoker) delegate
                                        {
                                            rlog.Visible = true;
                                            rlog.AppendText($"Update FileHash fault:{result3.Exception.InnerException.Message}");
                                            rlog.Height = 55;
                                        });
                                        break;
                                    }
                                }
                                myTransaction.Commit();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            rlog.Visible = true;
                            rlog.AppendText("Query ERR " + ex.Message + Environment.NewLine + qry);
                            rlog.Height  = 55;
                            badd.Enabled = true;
                        });
                    }
                }
                SetStatus(i + "/" + files.Length);
                UPDPB1(i);
                i++;
            }


            SQLiteCommand srvCommand = new SQLiteCommand(sqlcon);

            srvCommand.CommandText = $"INSERT INTO {_FTS_TABLE} VALUES('optimize','optimize')";
            var res = srvCommand.ExecuteNonQueryAsync();

            if (res.IsFaulted)
            {
                Invoke((MethodInvoker) delegate
                {
                    rlog.Visible = true;
                    rlog.AppendText($"optimize fault:{res.Exception.InnerException.Message}");
                    rlog.Height = 55;
                });
            }
            SetStatus("Ожидание");
            UPDPB1(0);
            GC.Collect();
            SetPBVisible(false);
            Task.Run(() => initDBFiles());
            Invoke((MethodInvoker) delegate
            {
                badd.Enabled = true;
            });
        }