/* 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.IO;
using System.Security;
namespace Alphaleonis.Win32.Filesystem
{
/// Represents information about a file system entry.
/// This class cannot be inherited.
///
[SerializableAttribute]
[SecurityCritical]
public sealed class FileSystemEntryInfo
{
/// Initializes a new instance of the class.
/// The NativeMethods.WIN32_FIND_DATA structure.
internal FileSystemEntryInfo(NativeMethods.WIN32_FIND_DATA findData)
{
Win32FindData = findData;
}
#region Properties
/// Gets the 8.3 version of the filename.
/// the 8.3 version of the filename.
public string AlternateFileName
{
get { return Win32FindData.cAlternateFileName; }
}
/// Gets the attributes.
/// The attributes.
public FileAttributes Attributes
{
get { return Win32FindData.dwFileAttributes; }
}
/// Gets the time this entry was created.
/// The time this entry was created.
public DateTime CreationTime
{
get { return CreationTimeUtc.ToLocalTime(); }
}
/// Gets the time, in coordinated universal time (UTC), this entry was created.
/// The time, in coordinated universal time (UTC), this entry was created.
public DateTime CreationTimeUtc
{
get { return DateTime.FromFileTimeUtc(Win32FindData.ftCreationTime); }
}
/// Gets the name of the file.
/// The name of the file.
public string FileName
{
get { return Win32FindData.cFileName; }
}
/// Gets the size of the file.
/// The size of the file.
public long FileSize
{
get { return NativeMethods.ToLong(Win32FindData.nFileSizeHigh, Win32FindData.nFileSizeLow); }
}
private string _fullPath;
/// The full path of the file system object.
public string FullPath
{
get { return _fullPath; }
set
{
LongFullPath = value;
_fullPath = Path.GetRegularPathCore(LongFullPath, GetFullPathOptions.None, false);
}
}
/// Gets a value indicating whether this instance is compressed.
/// if this instance is compressed; otherwise, .
///
/// It is not possible to change the compression status of a File object by using the SetAttributes method.
/// Instead, you must actually compress the file using either a compression tool or one of the classes in the namespace.
///
public bool IsCompressed
{
get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.Compressed) != 0; }
}
/// Gets a value indicating whether this instance is hidden, and thus is not included in an ordinary directory listing.
/// if this instance is hidden; otherwise, .
public bool IsHidden
{
get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.Hidden) != 0; }
}
/// Gets a value indicating whether this instance represents a directory.
/// if this instance represents a directory; otherwise, .
public bool IsDirectory
{
get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.Directory) != 0; }
}
/// Gets a value indicating whether this instance is encrypted (EFS).
/// if this instance is encrypted (EFS); otherwise, .
///
/// For a file, this means that all data in the file is encrypted.
/// For a directory, this means that encryption is the default for newly created files and directories.
///
public bool IsEncrypted
{
get { return Attributes != (FileAttributes) (-1) && (Attributes & FileAttributes.Encrypted) != 0; }
}
/// Gets a value indicating whether this instance is a mount point.
/// if this instance is a mount point; otherwise, .
public bool IsMountPoint
{
get { return ReparsePointTag == ReparsePointTag.MountPoint; }
}
/// Gets a value indicating whether this instance is offline. The data of the file is not immediately available.
/// if this instance is offline; otherwise, .
public bool IsOffline
{
get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.Offline) != 0; }
}
/// Gets a value indicating whether this instance is read-only.
/// if this instance is read-only; otherwise, .
public bool IsReadOnly
{
get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.ReadOnly) != 0; }
}
/// Gets a value indicating whether this instance contains a reparse point, which is a block of user-defined data associated with a file or a directory.
/// if this instance contains a reparse point; otherwise, .
public bool IsReparsePoint
{
get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.ReparsePoint) != 0; }
}
/// Gets a value indicating whether this instance is a symbolic link.
/// if this instance is a symbolic link; otherwise, .
public bool IsSymbolicLink
{
get { return ReparsePointTag == ReparsePointTag.SymLink; }
}
/// Gets the time this entry was last accessed.
/// The time this entry was last accessed.
public DateTime LastAccessTime
{
get { return LastAccessTimeUtc.ToLocalTime(); }
}
/// Gets the time, in coordinated universal time (UTC), this entry was last accessed.
/// The time, in coordinated universal time (UTC), this entry was last accessed.
public DateTime LastAccessTimeUtc
{
get { return DateTime.FromFileTimeUtc(Win32FindData.ftLastAccessTime); }
}
/// Gets the time this entry was last modified.
/// The time this entry was last modified.
public DateTime LastWriteTime
{
get { return LastWriteTimeUtc.ToLocalTime(); }
}
/// Gets the time, in coordinated universal time (UTC), this entry was last modified.
/// The time, in coordinated universal time (UTC), this entry was last modified.
public DateTime LastWriteTimeUtc
{
get { return DateTime.FromFileTimeUtc(Win32FindData.ftLastWriteTime); }
}
private string _longFullPath;
/// The full path of the file system object in Unicode (LongPath) format.
public string LongFullPath
{
get { return _longFullPath; }
private set { _longFullPath = Path.GetLongPathCore(value, GetFullPathOptions.None); }
}
/// Gets the reparse point tag of this entry.
/// The reparse point tag of this entry.
public ReparsePointTag ReparsePointTag
{
get { return IsReparsePoint ? Win32FindData.dwReserved0 : ReparsePointTag.None; }
}
/// Gets internal WIN32 FIND Data
internal NativeMethods.WIN32_FIND_DATA Win32FindData { get; private set; }
#endregion // Properties
#region Methods
/// Returns the of the instance.
/// The instance as a string.
public override string ToString()
{
return FullPath;
}
#endregion // Methods
}
}