Beispiel #1
0
        /// <summary>
        /// Opens the file
        /// </summary>
        /// <param name="fileAccess">Specifies whether the file should be opened in read-only or read/write mode</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A <see cref="Stream"/> which can be used to read from or write to the file</returns>
        public async Task <Stream> OpenAsync(FileAccess fileAccess, CancellationToken cancellationToken)
        {
            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            if (fileAccess == FileAccess.Read)
            {
                return(File.OpenRead(Path));
            }
            else if (fileAccess == FileAccess.ReadAndWrite)
            {
                return(File.Open(Path, FileMode.Open, System.IO.FileAccess.ReadWrite));
            }
            else
            {
                throw new ArgumentException("Unrecognized FileAccess value: " + fileAccess);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Checks whether a folder or file exists at the given location.
        /// </summary>
        /// <param name="name">The name of the file or folder to check for.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// A task whose result is the result of the existence check.
        /// </returns>
        public async Task <ExistenceCheckResult> CheckExistsAsync(string name, CancellationToken cancellationToken)
        {
            Requires.NotNullOrEmpty(name, "name");

            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            string checkPath = PortablePath.Combine(this.Path, name);

            if (File.Exists(checkPath))
            {
                return(ExistenceCheckResult.FileExists);
            }
            else if (Directory.Exists(checkPath))
            {
                return(ExistenceCheckResult.FolderExists);
            }
            else
            {
                return(ExistenceCheckResult.NotFound);
            }
        }