public void RemoveFile(string path) { path = FixPath(path); lock (files) { if (FileExists(path) && (GetFile(path).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { throw CommonExceptions.AccessDenied(path); } files.Remove(path); } }
/// <summary> /// Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten. /// </summary> /// <param name="path">The file to write to. </param> /// <param name="contents">The string to write to the file. </param> /// <param name="encoding">The encoding to apply to the string.</param> /// <exception cref="ArgumentException"><paramref name="path"/> is a zero-length string, contains only white space, or contains one or more invalid characters as defined by <see cref="Path.GetInvalidPathChars"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/> or contents is empty.</exception> /// <exception cref="PathTooLongException"> /// The specified path, file name, or both exceed the system-defined maximum length. /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. /// </exception> /// <exception cref="DirectoryNotFoundException">The specified path is invalid (for example, it is on an unmapped drive).</exception> /// <exception cref="IOException">An I/O error occurred while opening the file.</exception> /// <exception cref="UnauthorizedAccessException"> /// path specified a file that is read-only. /// -or- /// This operation is not supported on the current platform. /// -or- /// path specified a directory. /// -or- /// The caller does not have the required permission. /// </exception> /// <exception cref="FileNotFoundException">The file specified in <paramref name="path"/> was not found.</exception> /// <exception cref="NotSupportedException"><paramref name="path"/> is in an invalid format.</exception> /// <exception cref="System.Security.SecurityException">The caller does not have the required permission.</exception> /// <remarks> /// Given a string and a file path, this method opens the specified file, writes the string to the file using the specified encoding, and then closes the file. /// The file handle is guaranteed to be closed by this method, even if exceptions are raised. /// </remarks> public override void WriteAllText(string path, string contents, Encoding encoding) { mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); VerifyValueIsNotNull(path, "path"); if (mockFileDataAccessor.Directory.Exists(path)) { throw CommonExceptions.AccessDenied(path); } VerifyDirectoryExists(path); MockFileData data = contents == null ? new MockFileData(new byte[0]) : new MockFileData(contents, encoding); mockFileDataAccessor.AddFile(path, data); }
public void AddDirectory(string path) { var fixedPath = FixPath(path, true); var separator = Path.DirectorySeparatorChar.ToString(); lock (files) { if (FileExists(fixedPath) && (GetFile(fixedPath).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { throw CommonExceptions.AccessDenied(fixedPath); } var lastIndex = 0; var isUnc = StringOperations.StartsWith(fixedPath, @"\\") || StringOperations.StartsWith(fixedPath, @"//"); if (isUnc) { //First, confirm they aren't trying to create '\\server\' lastIndex = StringOperations.IndexOf(fixedPath, separator, 2); if (lastIndex < 0) { throw CommonExceptions.InvalidUncPath(nameof(path)); } /* * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles. * See PR https://github.com/System-IO-Abstractions/System.IO.Abstractions/pull/90 for conversation */ } while ((lastIndex = StringOperations.IndexOf(fixedPath, separator, lastIndex + 1)) > -1) { var segment = fixedPath.Substring(0, lastIndex + 1); if (!Directory.Exists(segment)) { SetEntry(segment, new MockDirectoryData()); } } var s = StringOperations.EndsWith(fixedPath, separator) ? fixedPath : fixedPath + separator; SetEntry(s, new MockDirectoryData()); } }