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 .
Printable View
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 .
Post the VB.NET code. I'll take a stab at it.
First , nevermind about the GoTo statement .
VB Code:
Private Declare Function SearchTreeForFile Lib "IMAGEHLP.DLL" (ByVal lpRootPath As String, ByVal lpInputName As String, ByVal lpOutputName As String) As Integer Private Const MAX_PATH As Short = 260 Public Function FindFile(ByRef RootPath As String, ByRef FileName As String) As String Dim lNullPos As Integer Dim lResult As Integer Dim sBuffer As String On Error GoTo FileFind_Error 'Allocate buffer sBuffer = Space(MAX_PATH * 2) 'Find the file lResult = SearchTreeForFile(RootPath, FileName, sBuffer) 'Trim null, if exists If lResult Then 'VB6 Code 'lNullPos = InStr(sBuffer, vbNullChar) 'MsgBox(lNullPos) ..NET code lNullPos = sBuffer.IndexOf(Nothing) + 1 If Not lNullPos Then 'VB6 Code 'sBuffer = VB.Left(sBuffer, lNullPos - 1) ..NET code sBuffer = sBuffer.Substring(0, lNullPos - 1) End If 'Return filename FindFile = sBuffer Else 'Nothing found FindFile = vbNullString End If Exit Function FileFind_Error: FindFile = vbNullString End Function
For C# part :
Thanks DevGrp .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
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 ) .
why not use System.IO.FileInfo? eg:
VB Code:
private void button1_Click(object sender, System.EventArgs e) { System.IO.FileInfo f=new System.IO.FileInfo("C:\\test.txt"); if (f.Exists) { MessageBox.Show("Found your file!"); } else { MessageBox.Show("it dont exsist!"); } }
you can also specify directories btw eg :
VB Code:
private void FindStuff(string dir ,string path) { System.IO.DirectoryInfo D=new System.IO.DirectoryInfo(dir); System.IO.FileInfo f=new System.IO.FileInfo(path); }
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:)
you mean something like this? :
VB Code:
[DllImport("IMAGEHLP.DLL")] public static extern int SearchTreeForFile(string lpRootPathtring, string lpInputName , StringBuilder lpOutputName ); private void button2_Click(object sender, System.EventArgs e) { FindFile("C:\\","myTextFile.txt"); } public void FindFile(string RootPath ,string FileName) { int lResult=0; StringBuilder sBuffer=new StringBuilder(256); lResult = SearchTreeForFile(RootPath ,FileName ,sBuffer); if (lResult!=0) { MessageBox.Show(sBuffer.ToString()); } }
It works only if the file is existed on the root partition , otherwise it returns nothing .
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.
How did you use it ? I typed C: and the file name but shows empty msgbox (although the file is on the root partition ) ??
The same thing happened for me, but if the file is not found I get an exception.
Check out my site, it's a great place for learning about Windows API calls.
www.PietschSoft.com/FunctionGuide
Hmm , so you didn't get it working (it doesn't look for files), did you ? :confused:Quote:
Originally posted by DevGrp
The same thing happened for me, but if the file is not found I get an exception.
Your website is amazing . But I can't seem to find anything related to what's here .Quote:
Originally posted by crpietschmann
Check out my site, it's a great place for learning about Windows API calls.
www.PietschSoft.com/FunctionGuide
Well I really dont know :D . I made a text file name setup.txt on my c drive. The program found it, but the Messagebox was blank.Quote:
Originally posted by Pirate
Hmm , so you didn't get it working (it doesn't look for files), did you ? :confused:
Quote:
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
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) .Quote:
Originally posted by DevGrp
Well I really dont know :D . I made a text file name setup.txt on my c drive. The program found it, but the Messagebox was blank.
:rolleyes: