⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileex.cs

📁 蓝牙通讯
💻 CS
📖 第 1 页 / 共 3 页
字号:
			}

			if(!b)
			{
				throw new WinAPIException("Failed to close handle");
			}

			return;
		}
		#endregion

		#endregion ---------------------------------------------

		#region FILETIME class
		private class FILETIME
		{
			private byte[] m_data;

			public FILETIME(long FileTime)
			{
				m_data = BitConverter.GetBytes(FileTime);
			}

			public FILETIME()
			{
				m_data = new byte[8];
			}


			public uint dwLowDateTime
			{
				get
				{
					return BitConverter.ToUInt32(m_data, 0);
				}
				set
				{
					Buffer.BlockCopy(BitConverter.GetBytes(value), 0, m_data, 0, 4);
				}
			}

			public uint dwHighDateTime
			{
				get
				{
					return BitConverter.ToUInt32(m_data, 4);
				}
				set
				{
					Buffer.BlockCopy(BitConverter.GetBytes(value), 0, m_data, 4, 4);
				}
			}

			public static implicit operator byte[](FILETIME f)
			{
				return f.m_data;
			}
		}
		#endregion

		/// <summary>
		/// Use by Seek for determining move start position
		/// <i>New in SDF version 1.1</i>
		/// <seealso cref="StreamInterfaceDriver.Seek"/>
		/// </summary>
		public enum MoveMethod
		{
			/// <summary>
			/// The start of the file
			/// </summary>
			FileBeginning,
			/// <summary>
			/// The current file pointer position
			/// </summary>
			CurrentPosition,
			/// <summary>
			/// The end of the file
			/// </summary>
			FileEnd
		}


		#region File P/Invokes

		[DllImport("coredll.dll", EntryPoint="CreateFile", SetLastError=true)]
		internal static extern int CreateFileCE(
			string lpFileName,
			uint dwDesiredAccess,
			uint dwShareMode,
			int lpSecurityAttributes,
			uint dwCreationDisposition,
			uint dwFlagsAndAttributes,
			int hTemplateFile);

		[DllImport("coredll.dll", EntryPoint="CloseHandle", SetLastError=true)]
		internal static extern int CloseHandleCE(IntPtr hObject);

		/// <summary>
		/// <i>New in SDF version 1.1</i>
		/// </summary>
		/// <param name="hDevice"></param>
		/// <param name="dwIoControlCode"></param>
		/// <param name="lpInBuffer"></param>
		/// <param name="nInBufferSize"></param>
		/// <param name="lpOutBuffer"></param>
		/// <param name="nOutBufferSize"></param>
		/// <param name="lpBytesReturned"></param>
		/// <param name="lpOverlapped"></param>
		/// <returns></returns>
		[DllImport("coredll.dll", EntryPoint="DeviceIoControl", SetLastError=true)]
		internal static extern int DeviceIoControlCE(
			IntPtr hDevice, 
			int dwIoControlCode, 
			byte[] lpInBuffer, 
			int nInBufferSize, 
			byte[] lpOutBuffer, 
			int nOutBufferSize, 
			ref int lpBytesReturned, 
			IntPtr lpOverlapped);

		/// <summary>
		/// <i>New in SDF version 1.1</i>
		/// </summary>
		/// <param name="hFile"></param>
		/// <param name="lDistanceToMove"></param>
		/// <param name="lpDistanceToMoveHigh"></param>
		/// <param name="dwMoveMethod"></param>
		/// <returns></returns>
		[DllImport("coredll.dll", EntryPoint="SetFilePointer", SetLastError=true)]
		internal static extern int SetFilePointerCE(
			IntPtr hFile, 
			int lDistanceToMove, 
			int lpDistanceToMoveHigh, 
			MoveMethod dwMoveMethod); 

		[DllImport("coredll.dll", EntryPoint="WriteFile", SetLastError=true)]
		internal static extern int WriteFileCE(
			IntPtr hFile,
			Byte[] lpBuffer,
			int nNumberOfBytesToWrite,
			ref int lpNumberOfBytesWritten,
			IntPtr lpOverlapped);

		[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
		internal static extern int ReadFileCE(
			IntPtr hFile,
			byte[] lpBuffer,
			int nNumberOfBytesToRead,
			ref int lpNumberOfBytesRead,
			IntPtr lpOverlapped);

		[DllImport("coredll", EntryPoint="GetFileAttributes", SetLastError=true)]
		internal static extern uint GetFileAttributes(string lpFileName);

		[DllImport("coredll", EntryPoint="SetFileAttributes", SetLastError=true)]
		internal static extern bool SetFileAttributes(string lpFileName, uint dwFileAttributes);

		[DllImport("coredll", EntryPoint="SetFileTime", SetLastError=true)]
		internal static extern bool SetFileTime(IntPtr hFile, byte[] lpCreationTime, byte[] lpLastAccessTime, byte[] lpLastWriteTime); 

		#endregion
	}
	#endregion

	#region File Access Enumeration
	/// <summary>
	/// CreateFile file access flags
	/// </summary>
	[Flags(),CLSCompliant(false)]
	public enum FileAccess : uint
	{
		/// <summary>
		/// Read access to the file.  Data can be read from the file.
		/// </summary>
		Read		= 0x80000000,
		/// <summary>
		/// Write access to the file.  Data can be written to the file.
		/// </summary>
		Write		= 0x40000000,
		/// <summary>
		/// Execute permission. The file can be executed.
		/// </summary>
		Execute		= 0x20000000,
		/// <summary>
		/// All permissions.
		/// </summary>
		All			= 0x10000000
	}

	public enum FileAccessEx
	{
		/// <summary>
		/// Read access to the file. Data can be read from the file. Combine with Write for read/write access.
		/// </summary>
		Read		= 1,
		/// <summary>
		/// Write access to the file.  Data can be written to the file.
		/// </summary>
		Write		= 2,
		/// <summary>
		/// Read and write access to the file. Data can be written to and read from the file.
		/// </summary>
		ReadWrite		= 3
	}
	#endregion

	#region File Share
	/// <summary>
	/// CreateFile file share mode
	/// </summary>
	public enum FileShare : int
	{
		/// <summary>
		/// Declines sharing of the current file.
		/// Any request to open the file (by this process or another process) will fail until the file is closed.
		/// </summary>
		None		= 0,
		/// <summary>
		/// Allows subsequent opening of the file for reading.
		/// If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed.
		/// However, if this flag is specified additional permissions might still be needed to access the file.
		/// </summary>
		Read		= 1,
		/// <summary>
		/// Allows subsequent opening of the file for writing.
		/// If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed.
		/// However, if this flag is specified additional permissions might still be needed to access the file.
		/// </summary>
		Write		= 2,
		/// <summary>
		/// Allows subsequent opening of the file for reading or writing.
		/// If this flag is not specified, any request to open the file for writing or reading (by this process or another process) will fail until the file is closed.
		/// However, if this flag is specified additional permissions might still be needed to access the file.
		/// </summary>
		ReadWrite	= 3,
		/// <summary>
		/// Allows the file to be deleted.
		/// </summary>
		Delete		= 4,
	}
	#endregion

	#region File Create Disposition
	/// <summary>
	/// Specifies which action to take on files that exist, and which action to take when files do not exist.
	/// </summary>
	public enum FileCreateDisposition : int
	{
		/// <summary>
		/// Creates a new file.
		/// The function fails if the specified file already exists.
		/// </summary>
		CreateNew			= 1,
		/// <summary>
		/// Creates a new file.
		/// If the file exists, the function overwrites the file and clears the existing attributes.
		/// </summary>
		CreateAlways		= 2,
		/// <summary>
		/// Opens the file.
		/// The function fails if the file does not exist.
		/// </summary>
		OpenExisting		= 3,
		/// <summary>
		/// Opens the file, if it exists.
		/// If the file does not exist, the function creates the file as if dwCreationDisposition were <b>CreateNew</b>.
		/// </summary>
		OpenAlways			= 4,
		/// <summary>
		/// Opens the file.
		/// Once opened, the file is truncated so that its size is zero bytes. The calling process must open the file with at least Write access.
		/// </summary>
		TruncateExisting	= 5,
		/// <summary>
		/// 
		/// </summary>
		OpenForLoader		= 6
	}
	#endregion

	#region File Flags
	/// <summary>
	/// CreateFile file flags
	/// </summary>
	[CLSCompliant(false)]
	public enum FileFlags : uint
	{
		/// <summary>
		/// Instructs the system to write through any intermediate cache and go directly to disk.
		/// The system can still cache write operations, but cannot lazily flush them.
		/// </summary>
		WriteThrough				= 0x80000000,
		/// <summary>
		/// This flag is not supported; however, multiple read/write operations pending on a device at a time are allowed.
		/// </summary>
		Overlapped				= 0x40000000,
		/// <summary>
		/// Indicates that the file is accessed randomly.
		/// The system can use this as a hint to optimize file caching.
		/// </summary>
		RandomAccess				= 0x10000000,
		/// <summary>
		/// 
		/// </summary>
		SequentialScan			= 0x08000000,
		/// <summary>
		/// 
		/// </summary>
		DeleteOnClose			= 0x04000000
	}
	#endregion

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -