static void setRootDir(bool create) { lock (lockObject) { if (rootDir != null) { if (!create) { return; } if (Directory.Exists(rootDir)) { return; } } List <string> baseDirs = new List <string> { CompanyUserDataDir, CompanyCommonDataDir, Log.AppDir, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Path.GetTempPath() + Path.DirectorySeparatorChar + CompanyName + Path.DirectorySeparatorChar, }; if (Log.baseDirs != null) { baseDirs.InsertRange(0, Log.baseDirs); } foreach (string baseDir in baseDirs) { BaseDir = baseDir; rootDir = BaseDir + Path.DirectorySeparatorChar + rootDirName + RootDirNameSuffix; if (create) { try { if (!Directory.Exists(rootDir)) { FileSystemRoutines.CreateDirectory(rootDir); } string testFile = rootDir + Path.DirectorySeparatorChar + "test"; File.WriteAllText(testFile, "test"); File.Delete(testFile); break; } catch //(Exception e) { rootDir = null; } } } if (rootDir == null) { throw new Exception("Could not access any log directory."); } rootDir = PathRoutines.GetNormalizedPath(rootDir, false); if (Directory.Exists(rootDir) && deleteLogsOlderThanDays >= 0 && deletingOldLogsThread?.IsAlive != true) { deletingOldLogsThread = ThreadRoutines.Start(() => { Log.DeleteOldLogs(deleteLogsOlderThanDays, DeleteOldLogsDialog); });//to avoid a concurrent loop while accessing the log file from the same thread } } }
/// <summary> /// Actualize new parameters. /// </summary> /// <param name="parameters"></param> virtual public void Switch(Parameters parameters) { try { if (parameters.Synchronize) { if (string.IsNullOrWhiteSpace(parameters.SynchronizationFolder)) { throw new Exception("SynchronizationFolder is not set."); } //if (string.IsNullOrWhiteSpace(parameters.UploadFolderName)) // throw new Exception("UploadFolderName is not set."); //if (string.IsNullOrWhiteSpace(parameters.DownloadFolderName)) // throw new Exception("DownloadFolderName is not set."); this.parameters = parameters; //synchronizedSettingsFieldFullNames= synchronizedSettingsFieldFullNames.Distinct().ToList(); downloadFolder = FileSystemRoutines.CreateDirectory(parameters.SynchronizationFolder + Path.DirectorySeparatorChar + parameters.DownloadFolderName); uploadFolder = FileSystemRoutines.CreateDirectory(parameters.SynchronizationFolder + Path.DirectorySeparatorChar + parameters.UploadFolderName); //if (PathRoutines.IsDirWithinDir(downloadFolder, uploadFolder)) // throw new Exception("DownloadFolder cannot be within UploadFolder: \r\n" + downloadFolder + "\r\n" + uploadFolder); //if (PathRoutines.IsDirWithinDir(downloadFolder, uploadFolder)) // throw new Exception("UploadFolder cannot be within DownloadFolder: \r\n" + uploadFolder + "\r\n" + downloadFolder); appSetupFolder = FileSystemRoutines.CreateDirectory(parameters.SynchronizationFolder); if (pollingThread?.IsAlive != true) { pollingThread = ThreadRoutines.Start(polling); } } } catch (Exception e) { ErrorHandler(e); } }
static public void CopyFiles(string toDirectory) { lock (objectFullNames2serializable) { string d = FileSystemRoutines.CreateDirectory(toDirectory + System.IO.Path.DirectorySeparatorChar + CONFIG_FOLDER_NAME); foreach (Serializable s in objectFullNames2serializable.Values) { if (File.Exists(s.__File))//it can be absent if default settings are used still { File.Copy(s.__File, d + System.IO.Path.DirectorySeparatorChar + PathRoutines.GetFileName(s.__File)); } } } }
void save() { __TypeVersion = __Info.TypeVersion; Saving(); string s = Serialization.Json.Serialize(this, __Info.Indented, true, !__Info.NullSerialized, false /*!!!default values always must be stored*/); if (__Info.Endec != null) { s = __Info.Endec.Encrypt(s); } FileSystemRoutines.CreateDirectory(PathRoutines.GetFileDir(__Info.File)); File.WriteAllText(__Info.File, s); Saved(); }
public static void Save(string file, object o, bool indented = true, bool polymorphic = true, bool ignoreNullValues = true, bool ignoreDefaultValues = false) { FileSystemRoutines.CreateDirectory(PathRoutines.GetFileDir(file)); File.WriteAllText(file, Serialize(o, indented, polymorphic, ignoreNullValues, ignoreDefaultValues)); }
public static void Save(string file, object o, bool indented = true, bool polymorphic = true, bool ignoreNullProperties = true) { FileSystemRoutines.CreateDirectory(PathRoutines.GetDirFromPath(file)); File.WriteAllText(file, Serialize(o, indented, polymorphic, ignoreNullProperties)); }
void polling() { try { while (parameters.Synchronize) { foreach (string ssfn in synchronizedSettingsFieldFullNames) { SettingsFieldInfo sfi = Config.GetSettingsFieldInfo(ssfn); Settings settings = sfi.GetObject(); if (settings == null) { continue; } pollDownloadSettingsFile(settings); pollUploadSettingsFile(settings); } //foreach (Type sst in synchronizedSettingsTypes) //{ // List<SettingsFieldInfo> sfis = Config.GetSettingsFieldInfos(sst); // if (sfis.Count < 1) // throw new Exception("Settings type " + sst.FullName + " was not found."); // foreach (SettingsFieldInfo sfi in sfis) // { // Settings settings = sfi.GetObject(); // if (settings == null) // continue; // pollUploadSettingsFile(settings); // pollDownloadSettingsFile(settings); // } //} if (synchronizedFolders != null) { foreach (SynchronizedFolder sf in synchronizedFolders) { string synchronizationFolder = downloadFolder + Path.DirectorySeparatorChar + sf.SynchronizationFolderName; if (Directory.Exists(synchronizationFolder)) { foreach (string file in Directory.GetFiles(synchronizationFolder)) { if (sf.FileNameFilter.IsMatch(PathRoutines.GetFileName(file)) == false) { continue; } try { DateTime downloadLWT = File.GetLastWriteTime(file); if (downloadLWT.AddSeconds(100) > DateTime.Now)//it is being written { return; } string file2 = sf.Folder + Path.DirectorySeparatorChar + PathRoutines.GetFileName(file); if (File.Exists(file2) && downloadLWT <= File.GetLastWriteTime(file2)) { return; } copy(file, file2); onNewerFile(file); } catch (Exception e) { ErrorHandler(e); } } } foreach (string file in Directory.GetFiles(sf.Folder)) { if (sf.FileNameFilter.IsMatch(PathRoutines.GetFileName(file)) == false) { continue; } try { DateTime uploadLWT = File.GetLastWriteTime(file); if (uploadLWT.AddSeconds(10) > DateTime.Now)//it is being written { return; } string file2 = FileSystemRoutines.CreateDirectory(uploadFolder + Path.DirectorySeparatorChar + sf.SynchronizationFolderName) + Path.DirectorySeparatorChar + PathRoutines.GetFileName(file); if (File.Exists(file2) && uploadLWT <= File.GetLastWriteTime(file2)) { return; } copy(file, file2); } catch (Exception e) { ErrorHandler(e); } } } } string appSetupFile = null; foreach (string file in Directory.GetFiles(appSetupFolder)) { Match m = appSetupFileFilter?.Match(file); if (m?.Success != true) { continue; } if (!Version.TryParse(m.Groups[1].Value, out Version v)) { continue; } if (v > programVersion && v > lastAppVersion) { lastAppVersion = v; appSetupFile = file; } } if (appSetupFile != null) { AppNewSetupFile = appSetupFile; onNewerAppVersion(appSetupFile); return; } Thread.Sleep(parameters.PollingPeriodMss); } } catch (Exception e) { ErrorHandler(new Exception("Synchronization thread exited due to exception.", e)); } }
static void setWorkDir(bool create) { lock (lockObject) { if (workDir != null) { if (!create) { return; } if (Directory.Exists(workDir)) { return; } } List <string> baseDirs = new List <string> { Log.AppDir, CompanyUserDataDir, CompanyCommonDataDir, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), System.IO.Path.GetTempPath() + System.IO.Path.DirectorySeparatorChar + CompanyName + System.IO.Path.DirectorySeparatorChar, }; if (Log.primaryBaseDirs != null) { baseDirs.InsertRange(0, Log.primaryBaseDirs); } foreach (string baseDir in baseDirs) { workDir = baseDir + System.IO.Path.DirectorySeparatorChar + Log.ProcessName + WorkDirNameSuffix; if (create) { try { if (!Directory.Exists(workDir)) { FileSystemRoutines.CreateDirectory(workDir); } string testFile = workDir + System.IO.Path.DirectorySeparatorChar + "test"; File.WriteAllText(testFile, "test"); File.Delete(testFile); Log.baseDir = baseDir; break; } catch //(Exception e) { workDir = null; } } } if (workDir == null) { throw new Exception("Could not access any log directory."); } workDir = PathRoutines.GetNormalizedPath(workDir, false); if (Directory.Exists(workDir) && deleteLogsOlderDays >= 0) { if (deletingOldLogsThread?.TryAbort(1000) == false) { throw new Exception("Could not abort deletingOldLogsThread"); } deletingOldLogsThread = ThreadRoutines.Start(() => { Log.DeleteOldLogs(deleteLogsOlderDays, DeleteOldLogsDialog); });//to avoid a concurrent loop while accessing the log file from the same thread } else { throw new Exception("Could not create log folder!"); } } // deletingOldLogsThread?.Join(); }