/// <summary> /// ThreadMainFindFiles - Main thread for file find thread /// </summary> private void ThreadMainFindFiles() { int cTicks = Environment.TickCount; itReason = INVOKE_FINDFILES.FINDFILE_QUERYSTARTPATH; m_ctlInvokeTarget.Invoke(m_deleCallback); string strPath = strBuffer; AddProgramsInDirectory(strPath); int cSeconds = (Environment.TickCount - cTicks + 500) / 1000; if (bThreadContinue) { // Send message for search time. strBuffer = "Ready - " + m_cFiles + " programs found in " + cSeconds + " seconds."; itReason = INVOKE_FINDFILES.STATUS_MESSAGE; m_ctlInvokeTarget.Invoke(m_deleCallback); // Trigger that search is done. itReason = INVOKE_FINDFILES.FINDFILE_COMPLETE; m_ctlInvokeTarget.Invoke(m_deleCallback); } }
/// <summary> /// FetchAndDisplayError - Display error in status bar /// </summary> public void FetchAndDisplayError() { strBuffer = string.Empty; // Is this a RAPI error? int err = Rapi.CeRapiGetError(); if (err != Rapi.S_OK) { strBuffer = "RAPI Error (0x" + ((int)err).ToString("x") + ")"; } else { // Check for CE error. err = Rapi.CeGetLastError(); if (err != (int)Rapi.RAPI_ERROR.ERROR_FILE_NOT_FOUND) { strBuffer = "CE Error (code = " + err.ToString("x") + ")"; } } if (strBuffer != string.Empty) { itReason = INVOKE_FINDFILES.STATUS_MESSAGE; m_ctlInvokeTarget.Invoke(m_deleCallback); } // Trigger that thread has ended. m_thrd = null; }
FindFilesCallback(object sender, System.EventArgs e) { INVOKE_FINDFILES it = this.m_thrdFindFiles.itReason; switch (it) { case INVOKE_FINDFILES.FINDFILE_QUERYSTARTPATH: string strStart = txtStartDir.Text; if (strStart.EndsWith("\\") == false) { strStart = strStart + "\\"; } m_thrdFindFiles.strBuffer = strStart; break; case INVOKE_FINDFILES.FINDFILE_NEWFILE: lboxPrograms.Items.Add(m_thrdFindFiles.strBuffer); break; case INVOKE_FINDFILES.FINDFILE_COMPLETE: this.cmdFind.Enabled = true; break; case INVOKE_FINDFILES.STATUS_MESSAGE: sbarMain.Text = m_thrdFindFiles.strBuffer; break; } }
/// <summary> /// AddProgramsInDirectory - Recursive function to search /// into directory tree. /// </summary> /// <param name="strDir">Starting directory</param> /// <returns></returns> private bool AddProgramsInDirectory(string strDir) { Trace.WriteLine("FindPrograms: " + "AddProgramsInDirectory (" + strDir + ")"); // Update status bar through delegate function. strBuffer = "Searching in " + strDir + "..."; itReason = INVOKE_FINDFILES.STATUS_MESSAGE; m_ctlInvokeTarget.Invoke(m_deleCallback); // As we add programs, store directory names. ArrayList alDirectories = new ArrayList(); Rapi.CE_FIND_DATA fd = new Rapi.CE_FIND_DATA(); // Start our search. string strSearch = strDir + "*.*"; IntPtr hff = Rapi.CeFindFirstFile(strSearch, ref fd); if ((int)hff == -1) { FetchAndDisplayError(); } else { do { string strFileName = fd.cFileName; int iFlag = (int) Rapi.FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY; if ((fd.dwFileAttributes & iFlag) == iFlag) { alDirectories.Add(strDir + fd.cFileName); } else { if (strFileName.EndsWith(".EXE") || strFileName.EndsWith(".exe")) { m_cFiles++; strBuffer = strDir + fd.cFileName; itReason = INVOKE_FINDFILES.FINDFILE_NEWFILE; m_ctlInvokeTarget.Invoke(m_deleCallback); } } } while (bThreadContinue && Rapi.CeFindNextFile(hff, ref fd) != 0); if (bThreadContinue && m_bIncludeSubDirs) { foreach (string str in alDirectories) { AddProgramsInDirectory(str + "\\"); } } } Rapi.CeFindClose(hff); return(true); }
/// <summary> /// AddProgramsInDirectory - Recursive function to search /// into directory tree. /// </summary> /// <param name="strDir">Starting directory</param> /// <returns></returns> private bool AddProgramsInDirectory(string strDir) { Trace.WriteLine("FindPrograms: " + "AddProgramsInDirectory (" + strDir + ")"); // Update status bar through delegate function. strBuffer = "Searching in " + strDir + "..."; itReason = INVOKE_FINDFILES.STATUS_MESSAGE; m_ctlInvokeTarget.Invoke(m_deleCallback); // As we add programs, store directory names. ArrayList alDirectories = new ArrayList(); // Start our search. string strSearch = strDir + "*.*"; IntPtr pfdAllFiles = IntPtr.Zero; // Return pointer. int cFound = 0; // Return count of files. // Call looking for all files in current directory. Rapi.CeFindAllFiles(strSearch, Rapi.FAF.FAF_ATTRIBUTES | Rapi.FAF.FAF_NAME, ref cFound, ref pfdAllFiles); // Loop through all files found. IntPtr pfd = pfdAllFiles; while (cFound > 0) { // // Here is the secret sauce. This function uses a // Win32 pointer to create a .NET object // Rapi.CE_FIND_DATA fd = // Output .NET object (Rapi.CE_FIND_DATA) // Always cast this Marshal.PtrToStructure( // The function pfd, // Input Win32 ptr typeof(Rapi.CE_FIND_DATA)); // Output type string strFileName = fd.cFileName; int iFlag = (int) Rapi.FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY; if ((fd.dwFileAttributes & iFlag) == iFlag) { alDirectories.Add(strDir + fd.cFileName); } else { if (strFileName.EndsWith(".EXE") || strFileName.EndsWith(".exe")) { m_cFiles++; strBuffer = strDir + fd.cFileName; itReason = INVOKE_FINDFILES.FINDFILE_NEWFILE; m_ctlInvokeTarget.Invoke(m_deleCallback); } } // Get ready for next loop. pfd = (IntPtr)((int)pfd + Marshal.SizeOf(fd)); cFound--; } // Free memory returned by CeFindAllFiles. Rapi.CeRapiFreeBuffer(pfdAllFiles); if (bThreadContinue && m_bIncludeSubDirs) { foreach (string str in alDirectories) { AddProgramsInDirectory(str + "\\"); } } return(true); }