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

📄 fileex.cs

📁 蓝牙通讯
💻 CS
📖 第 1 页 / 共 3 页
字号:
//==========================================================================================
//
//		OpenNETCF.IO.FileEx
//		Copyright (c) 2003, OpenNETCF.org
//
//		This library is free software; you can redistribute it and/or modify it under
//		the terms of the OpenNETCF.org Shared Source License.
//
//		This library is distributed in the hope that it will be useful, but
//		WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//		FITNESS FOR A PARTICULAR PURPOSE. See the OpenNETCF.org Shared Source License
//		for more details.
//
//		You should have received a copy of the OpenNETCF.org Shared Source License
//		along with this library; if not, email licensing@opennetcf.org to request a copy.
//
//		If you wish to contact the OpenNETCF Advisory Board to discuss licensing, please
//		email licensing@opennetcf.org.
//
//		For general enquiries, email enquiries@opennetcf.org or visit our website at:
//		http://www.opennetcf.org
//
//==========================================================================================
using System;
using System.IO;
using System.Runtime.InteropServices;
using OpenNETCF.Win32;

namespace OpenNETCF.IO
{
	#region Object Store Information
	/// <summary>
	/// Describes the current status of the Object Store
	/// </summary>
	public struct ObjectStoreInformation
	{
		/// <summary>
		/// Size of the Object Store in Bytes
		/// </summary>
		public int StoreSize;
		/// <summary>
		/// Free space in the Object Store in Bytes
		/// </summary>
		public int FreeSize;

		/// <summary>
		/// This function retrieves the size of the object store and the amount of free space currently in the object store
		/// </summary>
		/// <returns>An <see cref="ObjectStoreInformation"/> struct with the current status of the Object Store.</returns>
		public static ObjectStoreInformation GetStoreInformation()
		{
			try
			{
				ObjectStoreInformation osinfo = new ObjectStoreInformation();

				GetStoreInformationCE(out osinfo);

				return osinfo;
			}
			catch(Exception)
			{
				throw new WinAPIException("Error retrieving ObjectStoreInformation.");
			}
		}

		[DllImport("coredll", EntryPoint="GetStoreInformation", SetLastError=true)]
		private static extern int GetStoreInformationCE(out ObjectStoreInformation lpsi);

	}
	#endregion

	#region File Ex
	/// <summary>
	/// Provides additional file related functionality.
	/// </summary>
	public class FileEx
	{
		private FileEx(){}

		/// <summary>
		/// Maximum length of Filepath string (in characters)
		/// </summary>
		public const int	MaxPath				= 260;
		internal const int	ERROR_NO_MORE_FILES		= 18;

		/// <summary>
		/// Represents an invalid native operating system handle.
		/// </summary>
		public const int	InvalidHandle	= -1;

		#region Base wrappers
		/// <summary>
		/// Creates a StreamWriter that appends UTF-8 encoded text to an existing file.
		/// </summary>
		/// <param name="path">The path to the file to append to.</param>
		/// <returns>A StreamWriter that appends UTF-8 encoded text to an existing file.</returns>
		public static StreamWriter AppendText(string path)
		{
			return File.AppendText(path);
		}

		/// <summary>
		/// Copies an existing file to a new file.  Overwriting a file of the same name is not allowed.
		/// </summary>
		/// <param name="sourceFileName">The file to copy.</param>
		/// <param name="destFileName">The name of the destination file. This cannot be a directory or an existing file.</param>
		public static void Copy(string sourceFileName, string destFileName)
		{
			File.Copy(sourceFileName, destFileName);
		}

		/// <summary>
		/// Copies an existing file to a new file. Overwriting a file of the same name is allowed.
		/// </summary>
		/// <param name="sourceFileName">The file to copy.</param>
		/// <param name="destFileName">The name of the destination file. This cannot be a directory or an existing file.</param>
		/// <param name="overwrite"><b>true</b> if the destination file can be overwritten; otherwise, <b>false</b>.</param>
		public static void Copy(string sourceFileName, string destFileName, bool overwrite)
		{
			File.Copy(sourceFileName, destFileName, overwrite);
		}

		/// <summary>
		/// Creates or overwrites the specified file.
		/// </summary>
		/// <param name="path">The name of the file.</param>
		/// <returns>A FileStream that provides read/write access to the specified file.</returns>
		public static FileStream Create(string path)
		{
			return File.Create(path);
		}

		/// <summary>
		/// Creates or overwrites the specified file.
		/// </summary>
		/// <param name="path">The name of the file.</param>
		/// <param name="bufferSize">The number of bytes buffered for reads and writes to the file.</param>
		/// <returns>A new file with the specified buffer size.</returns>
		public static FileStream Create(string path, int bufferSize)
		{
			return File.Create(path, bufferSize);
		}

		/// <summary>
		/// Creates or opens a file for writing UTF-8 encoded text.
		/// </summary>
		/// <param name="path">The file to be opened for writing.</param>
		/// <returns>A StreamWriter that writes to the specified file using UTF-8 encoding.</returns>
		/// <exception cref="ArgumentException">path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.</exception>
		public static StreamWriter CreateText(string path)
		{
			return File.CreateText(path);
		}

		/// <summary>
		/// Deletes the specified file. An exception is not thrown if the specified file does not exist.
		/// </summary>
		/// <param name="path">The name of the file to be deleted. </param>
		public static void Delete(string path)
		{
			File.Delete(path);
		}

		/// <summary>
		/// Determines whether the specified file exists.
		/// </summary>
		/// <param name="path">The file to check.</param>
		/// <returns><b>true</b> if the caller has the required permissions and path contains the name of an existing file; otherwise, <b>false</b>. This method also returns <b>false</b> if path is a null reference (Nothing in Visual Basic) or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns <b>false</b> regardless of the existence of path.</returns>
		public static bool Exists(string path)
		{
			return File.Exists(path);
		}

		/// <summary>
		/// Returns the creation date and time of the specified file or directory.
		/// </summary>
		/// <param name="path">The file or directory for which to obtain creation date and time information.</param>
		/// <returns><seealso cref="DateTime"/>A DateTime structure set to the creation date and time for the specified file or directory. This value is expressed in local time.</returns>
		public static DateTime GetCreationTime(string path)
		{
			return File.GetCreationTime(path);
		}

		/// <summary>
		/// Returns the creation date and time, in coordinated universal time (UTC), of the specified file or directory.
		/// </summary>
		/// <param name="path">The file or directory for which to obtain creation date and time information.</param>
		/// <returns>A DateTime structure set to the creation date and time for the specified file or directory. This value is expressed in UTC time.</returns>
		public static DateTime GetCreationTimeUtc(string path)
		{
			return File.GetCreationTime(path).ToUniversalTime();
		}
		
		/// <summary>
		/// Returns the date and time the specified file or directory was last accessed.
		/// </summary>
		/// <param name="path">The file or directory for which to obtain access date and time information.</param>
		/// <returns>A DateTime structure set to the date and time that the specified file or directory was last accessed. This value is expressed in local time.</returns>
		public static DateTime GetLastAccessTime(string path)
		{
			return File.GetLastAccessTime(path);
		}

		/// <summary>
		/// Returns the date and time the specified file or directory was last accessed.
		/// </summary>
		/// <param name="path">The file or directory for which to obtain access date and time information.</param>
		/// <returns>A DateTime structure set to the date and time that the specified file or directory was last accessed. This value is expressed in UTC time.</returns>
		public static DateTime GetLastAccessTimeUtc(string path)
		{
			return File.GetLastAccessTime(path).ToUniversalTime();
		}

		/// <summary>
		/// Returns the date and time the specified file or directory was last written to.
		/// </summary>
		/// <param name="path">The file or directory for which to obtain access date and time information.</param>
		/// <returns>A DateTime structure set to the date and time that the specified file or directory was last written to. This value is expressed in local time.</returns>
		public static DateTime GetLastWriteTime(string path)
		{
			return File.GetLastWriteTime(path);
		}

		/// <summary>
		/// Returns the date and time the specified file or directory was last written to.
		/// </summary>
		/// <param name="path">The file or directory for which to obtain access date and time information.</param>
		/// <returns>A DateTime structure set to the date and time that the specified file or directory was last written to. This value is expressed in UTC time.</returns>
		public static DateTime GetLastWriteTimeUtc(string path)
		{
			return File.GetLastWriteTime(path).ToUniversalTime();
		}

		/// <summary>
		/// Moves a specified file to a new location, providing the option to specify a new file name.
		/// </summary>
		/// <param name="sourceFileName">The name of the file to move.</param>
		/// <param name="destFileName">The new path for the file.</param>
		public static void Move(string sourceFileName, string destFileName)
		{
			File.Move(sourceFileName, destFileName);
		}


		/// <summary>
		/// Opens a FileStream on the specified path.
		/// </summary>
		/// <param name="path">The file to open.</param>
		/// <param name="mode">A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
		/// <returns>A FileStream on the specified path.</returns>
		public static FileStream Open(string path, FileMode mode)	
		{
			return File.Open(path, mode);
		}

		/// <summary>
		/// Opens a FileStream on the specified path.
		/// </summary>
		/// <param name="path">The file to open.</param>
		/// <param name="mode">A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
		/// <param name="access">A FileAccess value that specifies the operations that can be performed on the file.</param>
		/// <returns>A FileStream on the specified path, having the specified mode with read, write, or read/write access.</returns>
		public static FileStream Open(string path, FileMode mode, FileAccessEx access)
		{
			return File.Open(path, mode, (System.IO.FileAccess)access);
		}

		/// <summary>
		/// Opens a FileStream on the specified path.
		/// </summary>
		/// <param name="path">The file to open.</param>
		/// <param name="mode">A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
		/// <param name="access">A FileAccess value that specifies the operations that can be performed on the file.</param>
		/// <param name="share">A FileShare value specifying the type of access other threads have to the file. </param>
		/// <returns>A FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</returns>
		public static FileStream Open(string path, FileMode mode, FileAccessEx access, FileShare share)
		{
			return File.Open(path, mode, (System.IO.FileAccess)access, (System.IO.FileShare)share);
		}

		/// <summary>
		/// Opens an existing file for reading.
		/// </summary>
		/// <param name="path">The file to be opened for reading.</param>
		/// <returns>A read-only FileStream on the specified path.</returns>
		public static FileStream OpenRead(string path)
		{
			return File.OpenRead(path);
		}

		/// <summary>
		/// Opens an existing UTF-8 encoded text file for reading.
		/// </summary>
		/// <param name="path">The file to be opened for reading.</param>
		/// <returns>A StreamReader on the specified path.</returns>
		public static StreamReader OpenText(string path)
		{
			return File.OpenText(path);
		}

		/// <summary>
		/// Opens an existing file for writing.
		/// </summary>
		/// <param name="path">The file to be opened for writing.</param>
		/// <returns>A read/write, unshared FileStream object on the specified path.</returns>
		public static FileStream OpenWrite(string path)
		{
			return File.OpenWrite(path);
		}

		#endregion

		#region FileAttributes Get/Set
		/// <summary>
		/// Gets the FileAttributes of the file on the path.

⌨️ 快捷键说明

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