📄 fileex.cs
字号:
/// <seealso cref="FileAttributes"/>
/// </summary>
/// <param name="path">The path to the file.</param>
/// <returns>The FileAttributes of the file on the path, or -1 if the path or file is not found.</returns>
/// <exception cref="ArgumentException">path is empty, contains only white spaces, or contains invalid characters.</exception>
/// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.</exception>
/// <exception cref="NotSupportedException">path is in an invalid format.</exception>
/// <exception cref="DirectoryNotFoundException">The specified path is invalid, such as being on an unmapped drive.</exception>
public static FileAttributes GetAttributes(string path)
{
if(path.Length > 260)
{
throw new PathTooLongException();
}
if(path.Trim().Length == 0)
{
throw new ArgumentException();
}
uint attr = GetFileAttributes(path);
if(attr == 0xFFFFFFFF)
{
int e = Marshal.GetLastWin32Error();
if((e == 2) || (e == 3))
{
throw new FileNotFoundException();
}
else
{
throw new Exception("Unmanaged Error: " + e);
}
}
return (FileAttributes)attr;
}
/// <summary>
/// Sets the specified FileAttributes of the file on the specified path.
/// </summary>
/// <param name="path">The path to the file.</param>
/// <param name="fileAttributes"><seealso cref="FileAttributes"/>The desired FileAttributes, such as Hidden, ReadOnly, Normal, and Archive.</param>
public static void SetAttributes(string path, FileAttributes fileAttributes)
{
if(path.Length > 260)
{
throw new PathTooLongException();
}
if(path.Trim().Length == 0)
{
throw new ArgumentException();
}
if(! SetFileAttributes(path, (uint)fileAttributes))
{
int e = Marshal.GetLastWin32Error();
if((e == 2) || (e == 3))
{
throw new FileNotFoundException();
}
else
{
throw new Exception("Unmanaged Error: " + e);
}
}
}
#endregion
#region FileTime Sets
/// <summary>
/// Sets the date and time, in coordinated universal time (UTC), that the file was created.</summary>
/// <param name="path">The file for which to set the creation date and time information.</param>
/// <param name="creationTimeUtc">A DateTime containing the value to set for the creation date and time of path. This value is expressed in UTC time.</param>
public static void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
{
SetCreationTime(path, creationTimeUtc.ToLocalTime());
}
/// <summary>
/// Sets the date and time, in local time, that the file was created.</summary>
/// <param name="path">The file for which to set the creation date and time information.</param>
/// <param name="creationTime">A DateTime containing the value to set for the creation date and time of path. This value is expressed in local time.</param>
public static void SetCreationTime(string path, DateTime creationTime)
{
FILETIME ft;
IntPtr hFile = IntPtr.Zero;
if(path == null)
throw new ArgumentNullException();
if(path.Length > 260)
throw new PathTooLongException();
if(path.Trim().Length == 0)
throw new ArgumentException();
hFile = CreateFile(path, FileAccess.Write, FileShare.Write, FileCreateDisposition.OpenExisting, 0);
if((int)hFile == InvalidHandle)
{
int e = Marshal.GetLastWin32Error();
if((e == 2) || (e == 3))
{
throw new FileNotFoundException();
}
else
{
throw new Exception("Unmanaged Error: " + e);
}
}
ft = new FILETIME(creationTime.ToFileTime());
SetFileTime(hFile, ft, null, null);
CloseHandle(hFile);
}
/// <summary>
/// Sets the date and time, in coordinated universal time (UTC), that the file was last accessed.</summary>
/// <param name="path">The file for which to set the creation date and time information.</param>
/// <param name="lastAccessTimeUtc">A DateTime containing the value to set for the last access date and time of path. This value is expressed in UTC time.</param>
public static void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
{
SetLastAccessTime(path, lastAccessTimeUtc.ToLocalTime());
}
/// <summary>
/// Sets the date and time, in local time, that the file was last accessed.</summary>
/// <param name="path">The file for which to set the creation date and time information.</param>
/// <param name="lastAccessTime">A DateTime containing the value to set for the last access date and time of path. This value is expressed in local time.</param>
public static void SetLastAccessTime(string path, DateTime lastAccessTime)
{
FILETIME ft;
IntPtr hFile = IntPtr.Zero;
if(path == null)
throw new ArgumentNullException();
if(path.Length > 260)
throw new PathTooLongException();
if(path.Trim().Length == 0)
throw new ArgumentException();
hFile = CreateFile(path, FileAccess.Write, FileShare.Write, FileCreateDisposition.OpenExisting, 0);
if((int)hFile == InvalidHandle)
{
int e = Marshal.GetLastWin32Error();
if((e == 2) || (e == 3))
{
throw new FileNotFoundException();
}
else
{
throw new Exception("Unmanaged Error: " + e);
}
}
ft = new FILETIME(lastAccessTime.ToFileTime());
SetFileTime(hFile, null, ft, null);
CloseHandle(hFile);
}
/// <summary>
/// Sets the date and time, in coordinated universal time (UTC), that the file was last updated or written to.</summary>
/// <param name="path">The file for which to set the creation date and time information.</param>
/// <param name="lastWriteTimeUtc">A DateTime containing the value to set for the last write date and time of path. This value is expressed in UTC time.</param>
public static void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
{
SetLastWriteTime(path, lastWriteTimeUtc.ToLocalTime());
}
/// <summary>
/// Sets the date and time, in local time, that the file was last updated or written to.</summary>
/// <param name="path">The file for which to set the creation date and time information.</param>
/// <param name="lastWriteTime">A DateTime containing the value to set for the last write date and time of path. This value is expressed in local time.</param>
public static void SetLastWriteTime(string path, DateTime lastWriteTime)
{
FILETIME ft;
IntPtr hFile = IntPtr.Zero;
if(path == null)
throw new ArgumentNullException();
if(path.Length > 260)
throw new PathTooLongException();
if(path.Trim().Length == 0)
throw new ArgumentException();
hFile = CreateFile(path, FileAccess.Write, FileShare.Write, FileCreateDisposition.OpenExisting, 0);
if((int)hFile == InvalidHandle)
{
int e = Marshal.GetLastWin32Error();
if((e == 2) || (e == 3))
{
throw new FileNotFoundException();
}
else
{
throw new Exception("Unmanaged Error: " + e);
}
}
ft = new FILETIME(lastWriteTime.ToFileTime());
SetFileTime(hFile, null, ft, null);
CloseHandle(hFile);
}
#endregion
#region --------------- File API Calls ---------------
#region Create File
/// <summary>
/// Wrapper around the CreateFile API
/// </summary>
/// <param name="fileName">Path to the file or CE port name</param>
/// <param name="desiredAccess">Specifies the type of access to the object. An application can obtain read access, write access, read-write access, or device query access.</param>
/// <param name="shareMode">Specifies how the object can be shared.</param>
/// <param name="creationDisposition">Specifies which action to take on files that exist, and which action to take when files do not exist.</param>
/// <param name="flagsAndAttributes">Specifies the file attributes and flags for the file.</param>
/// <returns>Handle to the created file</returns>
[CLSCompliant(false)]
public static IntPtr CreateFile(string fileName,
FileAccess desiredAccess,
FileShare shareMode,
FileCreateDisposition creationDisposition,
int flagsAndAttributes)
{
IntPtr hFile = IntPtr.Zero;
hFile = (IntPtr)CreateFileCE(fileName, (uint)desiredAccess, (uint)shareMode, 0, (uint)creationDisposition, (uint)flagsAndAttributes, 0);
if((int)hFile == InvalidHandle)
{
throw new WinAPIException("Failed to Create File");
}
return hFile;
}
#endregion
#region Write File
/// <summary>
/// This function writes data to a file.
/// </summary>
/// <remarks> WriteFile starts writing data to the file at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written.</remarks>
/// <param name="hFile">Handle to the file to be written to. The file handle must have been created with GENERIC_WRITE access to the file.</param>
/// <param name="lpBuffer">Buffer containing the data to be written to the file.</param>
/// <param name="nNumberOfBytesToWrite">Number of bytes to write to the file.</param>
/// <param name="lpNumberOfBytesWritten">Number of bytes written by this function call. WriteFile sets this value to zero before doing any work or error checking.</param>
public static void WriteFile(IntPtr hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten)
{
bool b;
Core.CheckHandle(hFile);
b = Convert.ToBoolean(WriteFileCE(hFile, lpBuffer, nNumberOfBytesToWrite, ref lpNumberOfBytesWritten, IntPtr.Zero));
if(!b)
{
throw new WinAPIException("Write Failed");
}
}
#endregion
#region ReadFile
/// <summary>
/// This function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read.
/// </summary>
/// <param name="hFile">Handle to the file to be read. The file handle must have been created with GENERIC_READ access to the file. This parameter cannot be a socket handle.</param>
/// <param name="lpBuffer">Buffer that receives the data read from the file.</param>
/// <param name="nNumberOfBytesToRead">Number of bytes to be read from the file.</param>
/// <param name="lpNumberOfBytesRead">number of bytes read. ReadFile sets this value to zero before doing any work or error checking.</param>
public static void ReadFile(IntPtr hFile, byte[] lpBuffer, int nNumberOfBytesToRead, ref int lpNumberOfBytesRead)
{
bool b;
Core.CheckHandle(hFile);
b = Convert.ToBoolean(ReadFileCE(hFile, lpBuffer, nNumberOfBytesToRead, ref lpNumberOfBytesRead, IntPtr.Zero));
if(!b)
{
throw new WinAPIException("Write Failed");
}
}
#endregion
#region CloseHandle
/// <summary>
/// This function closes an open object handle
/// </summary>
/// <param name="hObject">Object Handle, Could be any of the following Objects:- Communications device, Mutex, Database, Process, Event, Socket, File or Thread</param>
public static void CloseHandle(IntPtr hObject)
{
bool b;
Core.CheckHandle(hObject);
try
{
b = Convert.ToBoolean(CloseHandleCE(hObject));
}
catch(Exception ex)
{
throw new WinAPIException(ex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -