public Settings(string root, string fileName, bool isMachineWideSettings) { if (String.IsNullOrEmpty(root)) { throw new ArgumentException("root cannot be null or empty", nameof(root)); } if (String.IsNullOrEmpty(fileName)) { throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(fileName)); } if (!FileSystemUtility.IsPathAFile(fileName)) { throw new ArgumentException(Resources.Settings_FileName_Cannot_Be_A_Path, nameof(fileName)); } Root = root; ConfigFileName = fileName; XDocument config = null; ExecuteSynchronized(() => config = XmlUtility.GetOrCreateDocument("configuration", ConfigFilePath)); ConfigXDocument = config; IsMachineWideSettings = isMachineWideSettings; }
/// <summary> /// Creates an instance of a SettingsFile /// </summary> /// <remarks>It will parse the specified document, /// if it doesn't exist it will create one with the default configuration.</remarks> /// <param name="directoryPath">path to the directory where the file is</param> /// <param name="fileName">name of config file</param> /// <param name="isMachineWide">specifies if the SettingsFile is machine wide.</param> /// <param name="isReadOnly">specifies if the SettingsFile is read only. If the config is machine wide, the value passed here is irrelevant. <see cref="IsReadOnly"/> will return <see langword="true"/> for every machine-wide config.</param> public SettingsFile(string directoryPath, string fileName, bool isMachineWide, bool isReadOnly) { if (string.IsNullOrEmpty(directoryPath)) { throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(directoryPath)); } if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(fileName)); } if (!FileSystemUtility.IsPathAFile(fileName)) { throw new ArgumentException(Resources.Settings_FileName_Cannot_Be_A_Path, nameof(fileName)); } DirectoryPath = directoryPath; FileName = fileName; ConfigFilePath = Path.GetFullPath(Path.Combine(DirectoryPath, FileName)); IsMachineWide = isMachineWide; IsReadOnly = IsMachineWide || isReadOnly; XDocument config = null; ExecuteSynchronized(() => { config = XmlUtility.GetOrCreateDocument(CreateDefaultConfig(), ConfigFilePath); }); _xDocument = config; _rootElement = new NuGetConfiguration(_xDocument.Root, origin: this); }
public static Tuple <string, string> GetFileNameAndItsRoot(string root, string settingsPath) { string fileName = null; if (Path.IsPathRooted(settingsPath)) { root = Path.GetDirectoryName(settingsPath); fileName = Path.GetFileName(settingsPath); } else if (!FileSystemUtility.IsPathAFile(settingsPath)) { var fullPath = Path.Combine(root ?? String.Empty, settingsPath); root = Path.GetDirectoryName(fullPath); fileName = Path.GetFileName(fullPath); } else { fileName = settingsPath; } return(new Tuple <string, string>(fileName, root)); }