Results 1 to 18 of 18

Thread: Search for files via API ?

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Search for files via API ?

    I got this part working in VB.NET , for many reasons , I couldn't convert it to C#. Anyone got code that looks for files via API ?

    Thanks .

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Post the VB.NET code. I'll take a stab at it.

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    First , nevermind about the GoTo statement .
    VB Code:
    1. Private Declare Function SearchTreeForFile Lib "IMAGEHLP.DLL" (ByVal lpRootPath As String, ByVal lpInputName As String, ByVal lpOutputName As String) As Integer
    2.  
    3.     Private Const MAX_PATH As Short = 260
    4.  
    5.     Public Function FindFile(ByRef RootPath As String, ByRef FileName As String) As String
    6.  
    7.         Dim lNullPos As Integer
    8.         Dim lResult As Integer
    9.         Dim sBuffer As String
    10.  
    11.         On Error GoTo FileFind_Error
    12.  
    13.         'Allocate buffer
    14.         sBuffer = Space(MAX_PATH * 2)
    15.  
    16.         'Find the file
    17.         lResult = SearchTreeForFile(RootPath, FileName, sBuffer)
    18.  
    19.         'Trim null, if exists
    20.         If lResult Then
    21.             'VB6 Code
    22.             'lNullPos = InStr(sBuffer, vbNullChar)
    23.             'MsgBox(lNullPos)
    24.  
    25.             ..NET code
    26.             lNullPos = sBuffer.IndexOf(Nothing) + 1
    27.  
    28.             If Not lNullPos Then
    29.                 'VB6 Code
    30.                 'sBuffer = VB.Left(sBuffer, lNullPos - 1)
    31.  
    32.                 ..NET code
    33.                 sBuffer = sBuffer.Substring(0, lNullPos - 1)
    34.             End If
    35.             'Return filename
    36.             FindFile = sBuffer
    37.         Else
    38.             'Nothing found
    39.             FindFile = vbNullString
    40.  
    41.         End If
    42.  
    43.         Exit Function
    44.  
    45. FileFind_Error:
    46.         FindFile = vbNullString
    47.  
    48.     End Function





    For C# part :

    Code:
    [DllImport("IMAGEHLP.DLL")]
    public static extern int SearchTreeForFile(string lpRootPathtring, string lpInputName , string lpOutputName ) ;
    private const short MAX_PATH  = 260;
    
    //The rest is junk..lol
    Thanks DevGrp .

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I thought , it'll be easier job to use this ready proj to try out .

    Usage : you'll find two textboxes , in the 1# type : C: (without backslash) , in the 2# type file name : setup.txt , then click the botton , if the file was found , msgbox will popup , if not , empty msgbox will show up . (don't worry , it won't take much time .It's damn fast ) .
    Attached Files Attached Files

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    why not use System.IO.FileInfo? eg:
    VB Code:
    1. private void button1_Click(object sender, System.EventArgs e)
    2. {
    3.     System.IO.FileInfo f=new System.IO.FileInfo("C:\\test.txt");
    4.     if (f.Exists)
    5.     {
    6.         MessageBox.Show("Found your file!");
    7.     }
    8.     else
    9.     {
    10.                     MessageBox.Show("it dont exsist!");
    11.     }
    12. }
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you can also specify directories btw eg :
    VB Code:
    1. private void FindStuff(string dir ,string path)
    2. {
    3.             System.IO.DirectoryInfo D=new System.IO.DirectoryInfo(dir);
    4.             System.IO.FileInfo f=new System.IO.FileInfo(path);
    5. }
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    API is faster (very fast) . Your code needs the user to provide the full path , I mean it won't search all folder as the API . I know I can search the entire folders but still slow . Can you help me to convert VB code to C# ?

    Thanks dynamic_sysop

  8. #8
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you mean something like this? :
    VB Code:
    1. [DllImport("IMAGEHLP.DLL")]
    2. public static extern int SearchTreeForFile(string lpRootPathtring, string lpInputName , StringBuilder lpOutputName );
    3.  
    4.  
    5. private void button2_Click(object sender, System.EventArgs e)
    6. {
    7. FindFile("C:\\","myTextFile.txt");
    8. }
    9.  
    10. public void FindFile(string RootPath ,string FileName)
    11. {
    12.  
    13. int lResult=0;
    14. StringBuilder sBuffer=new StringBuilder(256);
    15.            
    16. lResult = SearchTreeForFile(RootPath ,FileName ,sBuffer);
    17.  
    18. if (lResult!=0)
    19. {
    20. MessageBox.Show(sBuffer.ToString());
    21. }
    22. }
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It works only if the file is existed on the root partition , otherwise it returns nothing .

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    When I download the program, it was getting errors, so I made a few changes and it compiled. Everything seem to be working, except that I'm getting an exception if the file is not found. I cant seem to find a work around for the Space function.

    I also add a delegate and a callback function to do the search asynchronously, because it was whiting out the screen.

    Here is the update.
    Attached Files Attached Files

  11. #11

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    How did you use it ? I typed C: and the file name but shows empty msgbox (although the file is on the root partition ) ??

  12. #12
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    The same thing happened for me, but if the file is not found I get an exception.

  13. #13
    Hyperactive Member
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    279
    Check out my site, it's a great place for learning about Windows API calls.

    www.PietschSoft.com/FunctionGuide

  14. #14

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by DevGrp
    The same thing happened for me, but if the file is not found I get an exception.
    Hmm , so you didn't get it working (it doesn't look for files), did you ?

  15. #15

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by crpietschmann
    Check out my site, it's a great place for learning about Windows API calls.
    www.PietschSoft.com/FunctionGuide
    Your website is amazing . But I can't seem to find anything related to what's here .

  16. #16
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Originally posted by Pirate
    Hmm , so you didn't get it working (it doesn't look for files), did you ?
    Well I really dont know . I made a text file name setup.txt on my c drive. The program found it, but the Messagebox was blank.

  17. #17
    Hyperactive Member
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    279
    Originally posted by Pirate
    Your website is amazing . But I can't seem to find anything related to what's here .

    Thanks!!

    I'm sorry that there is nothing related to this there. At the moment I only have about 16 Function calls in the Function Guide. I am slowly adding like 1 or 2 a day. It's hard work. I have to add VB6, VB.NET, and C#.NET code, and I even have code examples for some of them.

    I encourage you to check it out. If you have certain Functions that you would like added to the Function Guide, just feel free to email me. I have an email link at the site. Any Function suggestions I get, will be added first.

    www.PietschSoft.com/FunctionGuide

  18. #18

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by DevGrp
    Well I really dont know . I made a text file name setup.txt on my c drive. The program found it, but the Messagebox was blank.
    So , it's not working properly . The msgbox will show up in both cases : found(file name on the msgbox) or not found(empty msgbox) .


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width