Beispiel #1
0
        public void AddFile(string path, VirtualFileData virtualFile)
        {
            var fixedPath = FixPath(path, true);

            lock (_files)
            {
                if (FileExists(fixedPath))
                {
                    var isReadOnly = (_files[fixedPath].Attributes & FileAttributes.ReadOnly) ==
                                     FileAttributes.ReadOnly;
                    var isHidden = (_files[fixedPath].Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;

                    if (isReadOnly || isHidden)
                    {
                        throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture,
                                                                            StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED") ?? throw new InvalidOperationException(), path));
                    }
                }

                var directoryPath = Path.GetDirectoryName(fixedPath);

                if (!_directory.Exists(directoryPath))
                {
                    AddDirectory(directoryPath);
                }

                _files[fixedPath] = virtualFile;
            }
        }
Beispiel #2
0
        private static VirtualFileData GetFileData(string path)
        {
            var file = new FileInfo(path);
            var data = new VirtualFileData(file.Length.ToString())
            {
                CreationTime  = file.CreationTime,
                LastWriteTime = file.LastWriteTime
            };

            return(data);
        }
Beispiel #3
0
        /// <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)
        {
            VirtualFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
            VerifyValueIsNotNull(path, "path");

            if (VirtualFileDataAccessor.Directory.Exists(path))
            {
                throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"), path));
            }

            VerifyDirectoryExists(path);

            VirtualFileData data = contents == null ? new VirtualFileData(new byte[0]) : new VirtualFileData(contents, encoding);

            VirtualFileDataAccessor.AddFile(path, data);
        }
Beispiel #4
0
        private DateTime GetTimeFromFile(string path, Func <VirtualFileData, DateTime> existingFileFunction, Func <DateTime> nonExistingFileFunction)
        {
            DateTime        result;
            VirtualFileData file = VirtualFileDataAccessor.GetFile(path);

            if (file != null)
            {
                result = existingFileFunction(file);
            }
            else
            {
                result = nonExistingFileFunction();
            }

            return(result);
        }