//Создает новый файл из шаблона, если его еще нет или версия не совпадает с шаблоном //Возвращает 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); }