/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* 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;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Security;
namespace Alphaleonis.Win32.Filesystem
{
/// Provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of objects. This class cannot be inherited.
[SerializableAttribute]
public sealed partial class FileInfo : FileSystemInfo
{
#region Constructors
#region .NET
/// Initializes a new instance of the class, which acts as a wrapper for a file path.
/// The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.
/// This constructor does not check if a file exists. This constructor is a placeholder for a string that is used to access the file in subsequent operations.
public FileInfo(string fileName) : this(null, fileName, PathFormat.RelativePath)
{
}
#endregion // .NET
#region AlphaFS
#region Non-Transactional
/// [AlphaFS] Initializes a new instance of the class, which acts as a wrapper for a file path.
/// The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.
/// Indicates the format of the path parameter(s).
/// This constructor does not check if a file exists. This constructor is a placeholder for a string that is used to access the file in subsequent operations.
public FileInfo(string fileName, PathFormat pathFormat) : this(null, fileName, pathFormat)
{
}
#endregion // Non-Transactional
#region Transactional
/// [AlphaFS] Initializes a new instance of the class, which acts as a wrapper for a file path.
/// The transaction.
/// The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.
/// This constructor does not check if a file exists. This constructor is a placeholder for a string that is used to access the file in subsequent operations.
public FileInfo(KernelTransaction transaction, string fileName) : this(transaction, fileName, PathFormat.RelativePath)
{
}
/// [AlphaFS] Initializes a new instance of the class, which acts as a wrapper for a file path.
/// The transaction.
/// The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.
/// Indicates the format of the path parameter(s).
/// This constructor does not check if a file exists. This constructor is a placeholder for a string that is used to access the file in subsequent operations.
public FileInfo(KernelTransaction transaction, string fileName, PathFormat pathFormat)
{
InitializeCore(false, transaction, fileName, pathFormat);
_name = Path.GetFileName(Path.RemoveTrailingDirectorySeparator(fileName, false), pathFormat != PathFormat.LongFullPath);
}
#endregion // Transacted
#endregion // AlphaFS
#endregion // Constructors
#region Properties
#region .NET
#region Directory
/// Gets an instance of the parent directory.
/// A object representing the parent directory of this file.
/// To get the parent directory as a string, use the DirectoryName property.
///
public DirectoryInfo Directory
{
get
{
string dirName = DirectoryName;
return dirName == null ? null : new DirectoryInfo(Transaction, dirName, PathFormat.FullPath);
}
}
#endregion // Directory
#region DirectoryName
/// Gets a string representing the directory's full path.
/// A string representing the directory's full path.
///
/// To get the parent directory as a DirectoryInfo object, use the Directory property.
/// When first called, FileInfo calls Refresh and caches information about the file.
/// On subsequent calls, you must call Refresh to get the latest copy of the information.
///
///
public string DirectoryName
{
[SecurityCritical] get { return Path.GetDirectoryName(FullPath, false); }
}
#endregion // DirectoryName
#region Exists
/// Gets a value indicating whether the file exists.
/// if the file exists; otherwise, .
///
/// The property returns if any error occurs while trying to determine if the specified file exists.
/// This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters,
/// a failing or missing disk, or if the caller does not have permission to read the file.
///
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public override bool Exists
{
[SecurityCritical]
get
{
try
{
if (DataInitialised == -1)
Refresh();
FileAttributes attrs = Win32AttributeData.dwFileAttributes;
return DataInitialised == 0 && attrs != (FileAttributes) (-1) && (attrs & FileAttributes.Directory) == 0;
}
catch
{
return false;
}
}
}
#endregion // Exists
#region IsReadOnly
/// Gets or sets a value that determines if the current file is read only.
/// if the current file is read only; otherwise, .
///
/// Use the IsReadOnly property to quickly determine or change whether the current file is read only.
/// When first called, FileInfo calls Refresh and caches information about the file.
/// On subsequent calls, you must call Refresh to get the latest copy of the information.
///
///
///
public bool IsReadOnly
{
get { return EntryInfo == null || EntryInfo.IsReadOnly; }
set
{
if (value)
Attributes |= FileAttributes.ReadOnly;
else
Attributes &= ~FileAttributes.ReadOnly;
}
}
#endregion // IsReadOnly
#region Length
/// Gets the size, in bytes, of the current file.
/// The size of the current file in bytes.
///
/// The value of the Length property is pre-cached
/// To get the latest value, call the Refresh method.
///
///
///
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
public long Length
{
[SecurityCritical]
get
{
if (DataInitialised == -1)
{
Win32AttributeData = new NativeMethods.WIN32_FILE_ATTRIBUTE_DATA();
Refresh();
}
// MSDN: .NET 3.5+: IOException: Refresh cannot initialize the data.
if (DataInitialised != 0)
NativeError.ThrowException(DataInitialised, LongFullName);
FileAttributes attrs = Win32AttributeData.dwFileAttributes;
// MSDN: .NET 3.5+: FileNotFoundException: The file does not exist or the Length property is called for a directory.
if (attrs == (FileAttributes) (-1))
NativeError.ThrowException(Win32Errors.ERROR_FILE_NOT_FOUND, LongFullName);
// MSDN: .NET 3.5+: FileNotFoundException: The file does not exist or the Length property is called for a directory.
if ((attrs & FileAttributes.Directory) == FileAttributes.Directory)
NativeError.ThrowException(Win32Errors.ERROR_FILE_NOT_FOUND, string.Format(CultureInfo.CurrentCulture, Resources.Target_File_Is_A_Directory, LongFullName));
return Win32AttributeData.FileSize;
}
}
#endregion // Length
#region Name
private string _name;
/// Gets the name of the file.
/// The name of the file.
///
/// The name of the file includes the file extension.
/// When first called, calls Refresh and caches information about the file.
/// On subsequent calls, you must call Refresh to get the latest copy of the information.
/// The name of the file includes the file extension.
///
public override string Name
{
get { return _name; }
}
#endregion // Name
#endregion // .NET
#endregion // Properties
}
}