//Проверяет, что файл file является файлом типа fileType (в SysSubTab параметр FileOptions\FileType) //Также производится проверка на содержание в файле списка указанных таблиц tables //Возвращает true, если проверка прошла удачно public static bool Check(string file, string fileType, IEnumerable <string> tables = null) { try { if (file.IsEmpty() || !new FileInfo(file).Exists) { return(false); } if (!fileType.IsEmpty()) { using (var daodb = new DaoDb(file)) using (var sys = new SysTabl(daodb)) if (sys.SubValue("FileOptions", "FileType") != fileType) { return(false); } } var en = new DBEngine(); var db = en.OpenDatabase(file); try { var missing = new SortedSet <string>(); if (tables != null) { foreach (var table in tables) { missing.Add(table); } foreach (var t in db.TableDefs) { string s = ((TableDef)t).Name; if (missing.Contains(s)) { missing.Remove(s); } } } return(missing.Count == 0); } finally { try { try { db.Close(); } catch { } db = null; en = null; GC.Collect(); } catch { } } } catch { return(false); } }
//Создает новый файл из шаблона, если его еще нет или версия не совпадает с шаблоном //Возвращает true, если файл был скопирован public static bool FromTemplate(string template, //путь к шаблону string file, //путь к создаваемому файлу, ReplaceByTemplate replace, //когда заменять сущесвующий файл bool saveOld = false) //копировать старый файл в текущий каталог, добавляя на конце _1, _2 и т. д. { var f = new FileInfo(file); if (!f.Directory.Exists) { f.Directory.Create(); } bool needCopy = !f.Exists; needCopy |= replace == ReplaceByTemplate.Always; if (!needCopy && replace == ReplaceByTemplate.IfNewVersion) { string st = null, sf = null; try { st = SysTabl.SubValueS(template, "AppOptions", "AppVersion"); } catch {} try { sf = SysTabl.SubValueS(file, "AppOptions", "AppVersion"); } catch { } needCopy |= st != sf; } if (needCopy) { if (saveOld && f.Exists) { bool b = true; string s = file.Substring(0, file.Length - 6); int i = 1; string ss = ""; while (b && i < 10000) { ss = s + "_" + (i++) + ".accdb"; b = new FileInfo(ss).Exists; } if (i <= 10000) { new FileInfo(file).MoveTo(ss); } else { new FileInfo(file).Delete(); } Thread.Sleep(2000); } new FileInfo(template).CopyTo(file, true); Thread.Sleep(500); } return(needCopy); }
//Создает новый файл из шаблона, если его еще нет или версия не совпадает с шаблоном и checkVersion = true //template - путь к шаблону, file - путь к создаваемому файлу, //saveOld - если true, то копирует старый файл в текущий каталог, добавляя на конце _1, _2 и т. д. //Возвращает true, если файл был скопирован public static bool FromTemplate(string template, string file, ReplaceByTemplate replace = ReplaceByTemplate.IfNewVersion, bool saveOld = false) { var f = new FileInfo(file); if (!f.Directory.Exists) { f.Directory.Create(); } bool needCopy = !f.Exists; needCopy |= replace == ReplaceByTemplate.Always; if (!needCopy && replace == ReplaceByTemplate.IfNewVersion) { string st = null, sf = null; try { st = SysTabl.SubValueS(template, "AppOptions", "AppVersion"); } catch {} try { sf = SysTabl.SubValueS(file, "AppOptions", "AppVersion"); } catch { } needCopy |= st != sf; } if (needCopy) { if (saveOld && f.Exists) { bool b = true; string s = file.Substring(0, file.Length - 6); int i = 1; string ss = ""; while (b && i < 10000) { ss = s + "_" + (i++) + ".accdb"; b = new FileInfo(ss).Exists; } if (i <= 10000) { new FileInfo(file).MoveTo(ss); } else { new FileInfo(file).Delete(); } Thread.Sleep(2000); } new FileInfo(template).CopyTo(file, true); Thread.Sleep(500); } return(needCopy); }