首页导航栏 - 网络工程 | 网站建设 | 设计在线 | 精彩图片 | 职业前线 | 创业之路 | 启步工具 | 教程资讯 | 休闲娱乐
启步网 > 设计在线 > 程序设计
C#使用WIN32API来遍历文件和目录[4]
作者: 不详 阅读: 来源:互连网 时间:2007-1-4 13:16:55
字体
    #region 声明WIN32API函数以及结构 **************************************
  
   [Serializable,
   System.Runtime.InteropServices.StructLayout
   (System.Runtime.InteropServices.LayoutKind.Sequential,
   CharSet = System.Runtime.InteropServices.CharSet.Auto
   ),
   System.Runtime.InteropServices.BestFitMapping(false)]
   private struct WIN32_FIND_DATA
   {
   public int dwFileAttributes;
   public int ftCreationTime_dwLowDateTime;
   public int ftCreationTime_dwHighDateTime;
   public int ftLastAccessTime_dwLowDateTime;
   public int ftLastAccessTime_dwHighDateTime;
   public int ftLastWriteTime_dwLowDateTime;
   public int ftLastWriteTime_dwHighDateTime;
   public int nFileSizeHigh;
   public int nFileSizeLow;
   public int dwReserved0;
   public int dwReserved1;
   [System.Runtime.InteropServices.MarshalAs
   (System.Runtime.InteropServices.UnmanagedType.ByValTStr,
   SizeConst = 260)]
   public string cFileName;
   [System.Runtime.InteropServices.MarshalAs
   (System.Runtime.InteropServices.UnmanagedType.ByValTStr,
   SizeConst = 14)]
   public string cAlternateFileName;
   }
  
   [System.Runtime.InteropServices.DllImport
   ("kernel32.dll",
   CharSet = System.Runtime.InteropServices.CharSet.Auto,
   SetLastError = true)]
   private static extern IntPtr FindFirstFile(string pFileName, ref WIN32_FIND_DATA pFindFileData);
  
   [System.Runtime.InteropServices.DllImport
   ("kernel32.dll",
   CharSet = System.Runtime.InteropServices.CharSet.Auto,
   SetLastError = true)]
   private static extern bool FindNextFile(IntPtr hndFindFile, ref WIN32_FIND_DATA lpFindFileData);
  
   [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
   private static extern bool FindClose(IntPtr hndFindFile);
  
   private static long ToLong( int height , int low)
   {
   long v = ( uint ) height ;
   v = v << 0x20;
   v = v | ( ( uint )low );
   return v;
   }
  
   private static void WinIOError(int errorCode, string str)
   {
   switch (errorCode)
   {
   case 80:
   throw new System.IO.IOException("IO_FileExists :" + str);
   case 0x57:
   throw new System.IO.IOException("IOError:" + MakeHRFromErrorCode(errorCode));
   case 0xce:
   throw new System.IO.PathTooLongException("PathTooLong:" + str );
   case 2:
   throw new System.IO.FileNotFoundException("FileNotFound:" + str);
   case 3:
   throw new System.IO.DirectoryNotFoundException("PathNotFound:" + str);
   case 5:
   throw new UnauthorizedAccessException("UnauthorizedAccess:" + str);
   case 0x20:
   throw new System.IO.IOException("IO_SharingViolation:" + str);
   }
   throw new System.IO.IOException("IOError:" + MakeHRFromErrorCode(errorCode));
   }
  
   private static int MakeHRFromErrorCode(int errorCode)
   {
   return (-2147024896 | errorCode);
   }
  
   #endregion
  
   #region 内部代码群 ****************************************************
  
   private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
   /// <summary>
   /// 查找处理的底层句柄
   /// </summary>
   private System.IntPtr intSearchHandler = INVALID_HANDLE_VALUE;
  
   private WIN32_FIND_DATA myData = new WIN32_FIND_DATA();
   /// <summary>
   /// 开始搜索标志
   /// </summary>
   private bool bolStartSearchFlag = false;
   /// <summary>
   /// 关闭内部句柄
   /// </summary>
   private void CloseHandler()
   {
   if (this.intSearchHandler != INVALID_HANDLE_VALUE)
   {
   FindClose(this.intSearchHandler);
   this.intSearchHandler = INVALID_HANDLE_VALUE;
   }
   }
   /// <summary>
   /// 开始搜索
   /// </summary>
   /// <returns>操作是否成功</returns>
   private bool StartSearch()
   {
   bolStartSearchFlag = true;
   bolIsEmpty = false;
   objCurrentObject = null;
   intLastErrorCode = 0;
  
   string strPath = System.IO.Path.Combine(strSearchPath, this.strSearchPattern);
   this.CloseHandler();
   intSearchHandler = FindFirstFile(strPath, ref myData);
   if (intSearchHandler == INVALID_HANDLE_VALUE)
   {
   intLastErrorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
   if (intLastErrorCode == 2)
   {
   bolIsEmpty = true;
   return false;
   }
   if( this.bolThrowIOException )
   WinIOError( intLastErrorCode , strSearchPath);
   else
   return false;
   }
   return true;
   }

责任编辑:一路狂奔
帐号:
密码:

Google
Google提供的相关资源
参与评论(条评论)
请遵守国家法律
笔名:
邮箱:
( 以上评论仅代表网友个人意见,不代表本站观点 )
相关资源
  • C#使用WIN32API来遍历文件和目录[5] [2007-1-4]
  • C#使用WIN32API来遍历文件和目录[3] [2007-1-4]
  • C#使用WIN32API来遍历文件和目录[2] [2007-1-4]
  • C#使用WIN32API来遍历文件和目录[1] [2007-1-4]
  •