Advertisement

09.03.2008 at 11:14AM PDT, ID: 23700240 | Points: 500
[x]
Attachment Details

Unicode File Search

Asked by merowinger in Visual Basic Programming, .NET, Microsoft Visual Basic.Net

Tags: , , , ,

Hi,
i'm just trying to replace the VB.NET internal function Directory.Getfiles() or GetDirectoies() with the API functions (FindFirstFile, FindNextFile, FindClose)
The Reason is that i need UNICODE to support more than 260 characters.
But i don't get code listed below running...either i get black result boxes or like in this case, i get access violations on the line where the path is defined!

I just want to list all files or all folders inside a directory!

A little bit help would be really cool!

AddOn: I know that i must use the FindFirstFileW function and \\?\ to define paths when long unicode paths shell working, but i first try to get this code running with "normal" paths!
Note: Please do not only post the code from google search here...i've already tried the most and they are not working in VB.NET as thiey are from further versions!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
Public Class Form1
 
    Private Structure FILETIME
        Dim dwLowDateTime As Long
        Dim dwHighDateTime As Long
    End Structure
    Private Structure WIN32_FIND_DATA
        Dim dwFileAttributes As Integer
        Dim ftCreationTime As FILETIME
        Dim ftLastAccessTime As FILETIME
        Dim ftLastWriteTime As FILETIME
        Dim nFileSizeHigh As Integer
        Dim nFileSizeLow As Integer
        Dim dwReserved0 As Integer
        Dim dwReserved1 As Integer
        <VBFixedString(MAX_PATH), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> Dim cFileName As String
        <VBFixedString(14), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=14)> Dim cAlternate As String
    End Structure
    Private Const MAX_PATH As Short = 260
    Private Const INVALID_HANDLE_VALUE As Short = -1
    Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, ByVal lpFindFileData As WIN32_FIND_DATA) As Long
    Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, ByVal lpFindFileData As WIN32_FIND_DATA) As Long
    Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
 
    Function TrimNull(ByVal sFileName As String) As String
        Dim i As Long
        ' Search for the first null character
        i = InStr(1, sFileName, vbNullChar)
        If i = 0 Then
            TrimNull = sFileName
        Else
            'Return the file name
            TrimNull = tLeft(sFileName, i - 1)
        End If
    End Function
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Buffer for output result
        Dim sBuff As String
        ' File search handle
        Dim iSearchHandle As Long
        ' File search buffer
        Dim pFindFileBuff As WIN32_FIND_DATA
        sBuff = vbNullString
        ' Find first file and create search handle
        iSearchHandle = FindFirstFile("C:\temp\*.*", pFindFileBuff)
        ' Check if FindFirstFile call was successful
        If iSearchHandle <> INVALID_HANDLE_VALUE Then
            ' Store first file name and date in the buffer
            sBuff = TrimNull(pFindFileBuff.cFileName) 
            ' Find the rest of files with the FindNextFile function
            Do While FindNextFile(iSearchHandle, pFindFileBuff)
                sBuff = sBuff & TrimNull(pFindFileBuff.cFileName)
            Loop
            ' Close file search handle
            Call FindClose(iSearchHandle)
        End If
        ' Show results
        MsgBox(sBuff)
    End Sub
 
    Public Function tLeft(ByVal sText As String, ByVal nLen As Integer) As String
        If nLen > sText.Length Then nLen = sText.Length
        Return (sText.Substring(0, nLen))
    End Function
End Class
[+][-]09.04.2008 at 05:49PM PDT, ID: 22394290

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.04.2008 at 07:59PM PDT, ID: 22394880

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.04.2008 at 08:00PM PDT, ID: 22394889

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.04.2008 at 11:16PM PDT, ID: 22395687

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.05.2008 at 04:15AM PDT, ID: 22397316

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.05.2008 at 05:03AM PDT, ID: 22397714

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.05.2008 at 08:33PM PDT, ID: 22405572

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.06.2008 at 03:21AM PDT, ID: 22406761

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.06.2008 at 08:57AM PDT, ID: 22407931

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.06.2008 at 09:05AM PDT, ID: 22407960

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.06.2008 at 07:09PM PDT, ID: 22409819

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.08.2008 at 12:46AM PDT, ID: 22415262

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.08.2008 at 05:17PM PDT, ID: 22423025

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.08.2008 at 10:58PM PDT, ID: 22424595

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.09.2008 at 05:23AM PDT, ID: 22426621

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.09.2008 at 08:12AM PDT, ID: 22428247

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.10.2008 at 10:44AM PDT, ID: 22440915

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.10.2008 at 11:51PM PDT, ID: 22446927

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.11.2008 at 08:00PM PDT, ID: 22456813

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.11.2008 at 10:47PM PDT, ID: 22457405

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.12.2008 at 04:22AM PDT, ID: 22458630

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906