public static bool UpdateSettings(SiteSettings newSettings) { // write settings to code or db // update Application-wide settings, only over-writing settings that users should edit lock (ClassifiedsHttpApplication.ClassifiedsApplicationSettings) { // Forms must be activated before appearing on the site ClassifiedsHttpApplication.ClassifiedsApplicationSettings.AdActivationRequired = newSettings.AdActivationRequired; // Store Photos in Database ClassifiedsHttpApplication.ClassifiedsApplicationSettings.StorePhotosInDatabase = newSettings.StorePhotosInDatabase; // ... else: use the following directory to save uploaded Photos: ClassifiedsHttpApplication.ClassifiedsApplicationSettings.ServerPhotoUploadDirectory = newSettings.ServerPhotoUploadDirectory; // Maximum Number of Photos to Upload ClassifiedsHttpApplication.ClassifiedsApplicationSettings.MaxPhotosPerAd = newSettings.MaxPhotosPerAd; // Maximum Number of Days for which an Form is active ClassifiedsHttpApplication.ClassifiedsApplicationSettings.MaxAdRunningDays = newSettings.MaxAdRunningDays; // Allow Users to edit their own Forms ClassifiedsHttpApplication.ClassifiedsApplicationSettings.AllowUsersToEditAds = newSettings.AllowUsersToEditAds; // Users to delete ads direclty in the database ClassifiedsHttpApplication.ClassifiedsApplicationSettings.AllowUsersToDeleteAdsInDB = newSettings.AllowUsersToDeleteAdsInDB; // Notifications to Administrators ClassifiedsHttpApplication.ClassifiedsApplicationSettings.AdminNotification = newSettings.AdminNotification; // Site Name ClassifiedsHttpApplication.ClassifiedsApplicationSettings.SiteName = newSettings.SiteName; // Contact Email Address for Site ClassifiedsHttpApplication.ClassifiedsApplicationSettings.SiteEmailAddress = newSettings.SiteEmailAddress; // Serialize to Xml Config File return SaveToXml(ClassifiedsHttpApplication.ClassifiedsApplicationSettings); } }
private static bool SaveToXml(SiteSettings settings) { if (settings == null) return false; HttpContext context = HttpContext.Current; if (context == null) return false; string configPath = context.Server.MapPath(XmlConfigFile); XmlSerializer xml = null; System.IO.FileStream fs = null; bool success = false; int numAttempts = 0; while (!success && numAttempts < 2) { try { numAttempts++; xml = new XmlSerializer(typeof(SiteSettings)); fs = new FileStream(configPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None); xml.Serialize(fs, settings); success = true; } catch { // if an exception is thrown, there might have been a sharing violation; // we wait and try again (max: two attempts) success = false; System.Threading.Thread.Sleep(1000); } } if (fs != null) fs.Close(); return success; }
public static SiteSettings LoadFromConfiguration() { SiteSettings s = LoadFromXml(); if (s == null) { s = new SiteSettings(); s.MaxPhotosPerAd = 5; s.StorePhotosInDatabase = true; s.ServerPhotoUploadDirectory = "Upload"; s.AdActivationRequired = false; s.AllowUsersToEditAds = true; s.AllowUsersToDeleteAdsInDB = true; s.MaxAdRunningDays = 21; s.AdminNotification = AdminNotificationSetting.None; s.SiteName = "ASP.NET Classifieds"; s.SiteEmailAddress = "*****@*****.**"; SaveToXml(s); } return s; }