コード例 #1
0
        /// <summary>
        /// Gets the specified File by the file name or a file nested inside a subdirectory owned by this directory
        /// </summary>
        /// <param name="pathToFile">Information.txt OR Lenovo\Information\Information.txt</param>
        /// <returns></returns>
        public Task <IFile> GetFileAsync(string pathToFile)
        {
            AssertDirectoryExists();

            return(Task.Factory.StartNew(() =>
            {
                IFile file = null;

                if (!String.IsNullOrWhiteSpace(pathToFile))
                {
                    // Get the full path to the desired file
                    string filePath = Environment.ExpandEnvironmentVariables(Path.Combine(this.FullPath, pathToFile));

                    if (File.Exists(filePath))
                    {
                        // Create new file info object, use this Directory's path and the passed in path to get it
                        var fileInfo = new FileInfo(filePath);

                        // Make sure the file info is null
                        if (fileInfo != null)
                        {
                            // Create new WinFile object using file info
                            file = new WinFile(fileInfo);
                        }
                    }
                }

                return file;
            }));
        }
コード例 #2
0
        /// <summary>
        /// Creates a file using the given file path, contents, and creation option
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="contents"></param>
        /// <param name="collisionOption"></param>
        /// <returns></returns>
        public Task <IFile> CreateFileAsync(string fileName, string contents, CreationOption collisionOption)
        {
            AssertDirectoryExists();

            return(Task.Factory.StartNew(() =>
            {
                IFile createdFile = null;
                string filePath = Environment.ExpandEnvironmentVariables(Path.Combine(this.FullPath, fileName));

                // File exists
                if (File.Exists(filePath))
                {
                    switch (collisionOption)
                    {
                    case CreationOption.OpenIfExists:
                        File.AppendAllText(filePath, contents);
                        break;

                    case CreationOption.ReplaceExisting:
                        File.WriteAllText(filePath, contents);
                        break;

                    case CreationOption.ThrowIfExists:
                        throw new Exceptions.FileAlreadyExistsException("The file to be created already exists in the target directory");

                    default:
                        goto case CreationOption.ReplaceExisting;
                    }
                }
                // File does not already exist
                else
                {
                    if (contents != null)
                    {
                        // Contents are not null, write all the text to the new file
                        File.WriteAllText(filePath, contents);
                    }
                    else
                    {
                        // Contents were null, just create a new, empty file
                        File.Create(filePath).Dispose();
                    }
                }

                // Create a new WinFile object using FileInfo for the filePath
                createdFile = new WinFile(new FileInfo(filePath));

                return createdFile;
            }));
        }