public DirectoryInfo[] GetDirectories() { bool finished = false; ArrayList diList = new ArrayList(); string searchPath=""; if(path.LastIndexOf("\\")==path.Length-1) searchPath=path+"*"; else searchPath=path+@"\*"; WIN32_FIND_DATA fileData = new WIN32_FIND_DATA(); HANDLE hSearch = (HANDLE)Win32.Kernel.FindFirstFile(searchPath, ref fileData); if (hSearch.ToInt32()==Win32.Kernel.INVALID_HANDLE_VALUE) return null; if(fileData.dwFileAttributes == Win32.Kernel.FILE_ATTRIBUTE_DIRECTORY) { diList.Add(new DirectoryInfo(path.TrimEnd('\\')+"\\"+fileData.cFileName)); } while(!finished) { if (Win32.Kernel.FindNextFile(hSearch, ref fileData)!=0) { if(fileData.dwFileAttributes == Win32.Kernel.FILE_ATTRIBUTE_DIRECTORY) { diList.Add(new DirectoryInfo(path.TrimEnd('\\')+"\\"+fileData.cFileName)); } } else { finished=!finished; } } DirectoryInfo[] di = new DirectoryInfo[diList.Count]; diList.CopyTo(di); return di; }
public static bool Exists(string path) { WIN32_FIND_DATA p = new WIN32_FIND_DATA(); int c = Win32.Kernel.FindFirstFile(setUnicodePath(path), ref p); if (c == Win32.Kernel.INVALID_HANDLE_VALUE || p.dwFileAttributes == Kernel.FILE_ATTRIBUTE_DIRECTORY) return false; return true; }
public static extern int FindNextFile(HANDLE hFindFile, WIN32_FIND_DATA lpFindFileData);
public static extern int FindFirstFile(string lpFileName, WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32", CharSet = CharSet.Unicode, EntryPoint="FindNextFileW")] public static extern int FindNextFile(HANDLE hFindFile, ref WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32", CharSet = CharSet.Unicode, EntryPoint="FindFirstFileW")] public static extern int FindFirstFile(string lpFileName, ref WIN32_FIND_DATA lpFindFileData);
private bool ExecutePath(string path) { bool res = false; string fileName, subPath; bool cancel; //��ֹ// if (this.terminated) return false; //����Դ·���������ļ�// WIN32_FIND_DATA lpFindFileData = new WIN32_FIND_DATA(); int hFindFile = Kernel.FindFirstFile(IOUtil.MakeLongFileName(path, finder.Filter), ref lpFindFileData); if (hFindFile != User.INVALID_HANDLE_VALUE) { this.tmpPath = path; try { if (provider != null) { provider.OnCallerAnalyzeItem(path, lpFindFileData); } do { //��ֹ// if (this.terminated) return false; //ȡ��Դ�ļ���// fileName = lpFindFileData.cFileName; //���Ϊ�ϲ�·������Continue// if (fileName.Equals(".") || fileName.Equals("..")) continue; //���ΪĿ¼����չ�����UpgradeDirectory���������¸���Ŀ¼// if ((lpFindFileData.dwFileAttributes & Kernel.FILE_ATTRIBUTE_DIRECTORY) == Kernel.FILE_ATTRIBUTE_DIRECTORY) { cancel = false; subPath = IOUtil.MakeLongFileName(path, fileName); if (provider != null) { provider.OnFileFinderGoToDirectory(subPath, lpFindFileData, ref cancel); } if (!cancel) { res = ExecutePath(subPath); if (!res) return false; } } //���Ϊ�ļ�// else { if (provider != null) { provider.OnCallerAnalyzeFile(path, lpFindFileData); } } } while (Kernel.FindNextFile(new IntPtr(hFindFile), ref lpFindFileData) != 0); if (this.milliSeconds > 0) Thread.Sleep(milliSeconds); res = true; } finally { Kernel.CloseHandle(new IntPtr(hFindFile)); } } return res; }
private void CurrentExecute(string path) { string fileName, currentPath, subPath; bool cancel; //����Ϊ��ǰ·��// currentPath = Directory.GetCurrentDirectory(); Directory.SetCurrentDirectory(path); try { //����Դ·���������ļ�// WIN32_FIND_DATA lpFindFileData = new WIN32_FIND_DATA(); int hFindFile = Kernel.FindFirstFile(this.filter, ref lpFindFileData); if (hFindFile != User.INVALID_HANDLE_VALUE) { try { if (provider != null) { provider.OnCallerAnalyzeItem(path, lpFindFileData); } do { //ȡ��Դ�ļ���// fileName = lpFindFileData.cFileName; //���Ϊ�ϲ�·������Continue// if (fileName.Equals(".") || fileName.Equals("..")) continue; //���ΪĿ¼����չ�����UpgradeDirectory���������¸���Ŀ¼// if ((lpFindFileData.dwFileAttributes & Kernel.FILE_ATTRIBUTE_DIRECTORY) == Kernel.FILE_ATTRIBUTE_DIRECTORY) { cancel = false; subPath = IOUtil.MakeLongFileName(path, fileName); if (provider != null) { provider.OnFileFinderGoToDirectory(subPath, lpFindFileData, ref cancel); } if (!cancel) CurrentExecute(subPath); } //���Ϊ�ļ�// else { if (provider != null) { provider.OnCallerAnalyzeFile(path, lpFindFileData); } } } while (Kernel.FindNextFile(new IntPtr(hFindFile), ref lpFindFileData) != 0); } finally { Kernel.CloseHandle(new IntPtr(hFindFile)); } } } finally { Directory.SetCurrentDirectory(currentPath); } }
public FileInfo[] GetFiles() { bool finished = false; string searchPath=""; ArrayList fiList = new ArrayList(); WIN32_FIND_DATA fileData = new WIN32_FIND_DATA(); if(path.LastIndexOf("\\")==path.Length-1) searchPath=path+"*"; else searchPath=path+@"\*"; HANDLE hSearch = (HANDLE)Win32.Kernel.FindFirstFile(searchPath, ref fileData); if (hSearch.ToInt32() == Win32.Kernel.INVALID_HANDLE_VALUE) return null; if(fileData.dwFileAttributes != Win32.Kernel.FILE_ATTRIBUTE_DIRECTORY) { fiList.Add(new FileInfo(path.TrimEnd('\\')+"\\"+fileData.cFileName)); Console.WriteLine(fileData.cFileName); } while(!finished) { if (Win32.Kernel.FindNextFile(hSearch, ref fileData)!=0) { if(fileData.dwFileAttributes != Win32.Kernel.FILE_ATTRIBUTE_DIRECTORY) { fiList.Add(new FileInfo(path.TrimEnd('\\')+"\\"+fileData.cFileName)); } } else { int err = Win32.Kernel.GetLastError(); finished=!finished; } } FileInfo[] fi = new FileInfo[fiList.Count]; fiList.CopyTo(fi); return fi; }