//
// Copyright (c) 2008-2011, Kenneth Bell
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
using System.IO;
namespace DiscUtils
{
///
/// Provides information about a file on a disc.
///
public sealed class DiscFileInfo : DiscFileSystemInfo
{
internal DiscFileInfo(DiscFileSystem fileSystem, string path)
: base(fileSystem, path) {}
///
/// Gets an instance of the parent directory.
///
public DiscDirectoryInfo Directory
{
get { return Parent; }
}
///
/// Gets a string representing the directory's full path.
///
public string DirectoryName
{
get { return Directory.FullName; }
}
///
/// Gets a value indicating whether the file exists.
///
public override bool Exists
{
get { return FileSystem.FileExists(Path); }
}
///
/// Gets or sets a value indicating whether the file is read-only.
///
public bool IsReadOnly
{
get { return (Attributes & FileAttributes.ReadOnly) != 0; }
set
{
if (value)
{
Attributes = Attributes | FileAttributes.ReadOnly;
}
else
{
Attributes = Attributes & ~FileAttributes.ReadOnly;
}
}
}
///
/// Gets the length of the current file in bytes.
///
public long Length
{
get { return FileSystem.GetFileLength(Path); }
}
///
/// Deletes a file.
///
public override void Delete()
{
FileSystem.DeleteFile(Path);
}
///
/// Creates a that appends text to the file represented by this .
///
/// The newly created writer.
public StreamWriter AppendText()
{
return new StreamWriter(Open(FileMode.Append));
}
///
/// Copies an existing file to a new file.
///
/// The destination file.
public void CopyTo(string destinationFileName)
{
CopyTo(destinationFileName, false);
}
///
/// Copies an existing file to a new file, allowing overwriting of an existing file.
///
/// The destination file.
/// Whether to permit over-writing of an existing file.
public void CopyTo(string destinationFileName, bool overwrite)
{
FileSystem.CopyFile(Path, destinationFileName, overwrite);
}
///
/// Creates a new file for reading and writing.
///
/// The newly created stream.
public Stream Create()
{
return Open(FileMode.Create);
}
///
/// Creates a new that writes a new text file.
///
/// A new stream writer that can write to the file contents.
public StreamWriter CreateText()
{
return new StreamWriter(Open(FileMode.Create));
}
///
/// Moves a file to a new location.
///
/// The new name of the file.
public void MoveTo(string destinationFileName)
{
FileSystem.MoveFile(Path, destinationFileName);
}
///
/// Opens the current file.
///
/// The file mode for the created stream.
/// The newly created stream.
/// Read-only file systems only support FileMode.Open.
public Stream Open(FileMode mode)
{
return FileSystem.OpenFile(Path, mode);
}
///
/// Opens the current file.
///
/// The file mode for the created stream.
/// The access permissions for the created stream.
/// The newly created stream.
/// Read-only file systems only support FileMode.Open and FileAccess.Read.
public Stream Open(FileMode mode, FileAccess access)
{
return FileSystem.OpenFile(Path, mode, access);
}
///
/// Opens an existing file for read-only access.
///
/// The newly created stream.
public Stream OpenRead()
{
return Open(FileMode.Open, FileAccess.Read);
}
///
/// Opens an existing file for reading as UTF-8 text.
///
/// The newly created reader.
public StreamReader OpenText()
{
return new StreamReader(OpenRead());
}
///
/// Opens a file for writing.
///
/// The newly created stream.
public Stream OpenWrite()
{
return Open(FileMode.Open, FileAccess.Write);
}
}
}