Ejemplo n.º 1
0
        /// <summary>
        /// Returns the fully qualified settings file name by preferring path of
        /// the executable. The path of user's local application data is returned
        /// in case of missing write permission on preferred path.
        /// </summary>
        /// <returns>
        /// The fully qualified settings file name.
        /// </returns>
        private static String GetSettingsFilename()
        {
            String site = Path.GetFullPath(Application.ExecutablePath);
            String file = Path.ChangeExtension(Path.GetFileName(site), ".conf");
            String path = Path.GetDirectoryName(site);

            if (SettingsSerializer.IsAccessPermitted(path, FileSystemRights.Write))
            {
                return(Path.Combine(path, file));
            }

            if (String.IsNullOrWhiteSpace(Application.CompanyName))
            {
                // May never happen, but safety first...
                throw new ArgumentException("Company name is invalid.");
            }

            if (String.IsNullOrWhiteSpace(Application.ProductName))
            {
                // May never happen, but safety first...
                throw new ArgumentException("Product name is invalid.");
            }

            path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            path = Path.Combine(path, Application.CompanyName, Application.ProductName);

            Directory.CreateDirectory(path);

            return(Path.Combine(path, file));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Tries to load settings using default settings file.
 /// </summary>
 /// <remarks>
 /// This method tries to load settings using default settings file.
 /// </remarks>
 /// <typeparam name="TInstance">
 /// The type to load the settings file content into.
 /// </typeparam>
 /// <param name="instance">
 /// The resulting instance of the settings file content.
 /// </param>
 /// <returns>
 /// In case of an error the method returns false. Otherwise the method
 /// returns true.
 /// </returns>
 public static Boolean Load <TInstance>(out TInstance instance) where TInstance : class, new()
 {
     return(SettingsSerializer.Load(SettingsSerializer.Filename, out instance));
 }
Ejemplo n.º 3
0
 static SettingsSerializer()
 {
     SettingsSerializer.filename = SettingsSerializer.GetSettingsFilename();
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Tries to save settings using default settings file.
 /// </summary>
 /// <remarks>
 /// This method tries to save settings using default settings file.
 /// </remarks>
 /// <typeparam name="TInstance">
 /// The type to save the settings file content from.
 /// </typeparam>
 /// <param name="instance">
 /// The instance of the settings.
 /// </param>
 /// <returns>
 /// In case of an error the method returns false. Otherwise the method
 /// returns true.
 /// </returns>
 public static Boolean Save <TInstance>(TInstance instance) where TInstance : class, new()
 {
     return(SettingsSerializer.Save(SettingsSerializer.Filename, instance));
 }