CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2011
    Posts
    6

    [RESOLVED] WINAPI FindFirstFile & FindNextFile problem

    Hi all,

    I'm trying to get the list of files in a directory using WINAPI functions FindFirstFile and FindNextFile.
    The problem is that WIN32_FIND_DATA.cFileName returns only the first character of the file name.
    This is a console application created in Microsoft Visual C# 2008 Express Edition.
    Here is the code. Do you have any ideas what is wrong?

    Thanks

    Code:
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    
    namespace DirectoryWINAPI
    {
        struct WIN32_FIND_DATA
        {
            public uint dwFileAttributes;
            public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
            public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
            public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
            public uint nFileSizeHigh;
            public uint nFileSizeLow;
            public uint dwReserved0;
            public uint dwReserved1;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string cFileName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
            public string cAlternateFileName;
        }
    
        class Program
        {
            [DllImport("kernel32", CharSet = CharSet.Auto)]
            public static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);
    
            [DllImport("kernel32", CharSet = CharSet.Auto)]
            public static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);
    
            [DllImport("kernel32", CharSet = CharSet.Auto)]
            public static extern bool FindClose(IntPtr hFindFile);
    
            static void Main(string[] args)
            {
                StreamWriter sw = new StreamWriter("dir.txt", false);
                string dir = Path.GetFullPath(System.IO.Directory.GetCurrentDirectory());
                WIN32_FIND_DATA wfd = new WIN32_FIND_DATA();
                IntPtr h = FindFirstFile((dir + @"\*.*"), out wfd);
                while (FindNextFile(h, out wfd))
                    sw.WriteLine(wfd.cFileName);
                FindClose(h);
                sw.Flush();
                sw.Close();
            }
        }
    }

  2. #2
    Join Date
    Mar 2011
    Posts
    6

    Re: WINAPI FindFirstFile & FindNextFile problem

    The problem is solved by adding:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

    before

    struct WIN32_FIND_DATA

    Thanks

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] WINAPI FindFirstFile & FindNextFile problem

    Are you aware that the .Net environment has directory and file classes built-in?

    See DirectoryInfo and FileInfo classes in Msdn.

  4. #4
    Join Date
    Mar 2011
    Posts
    6

    Re: [RESOLVED] WINAPI FindFirstFile & FindNextFile problem

    Yes, but it's much slower, especially if I use FileInfo.Length.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] WINAPI FindFirstFile & FindNextFile problem

    It might have to do with how you've written the C# code.

  6. #6
    Join Date
    Mar 2011
    Posts
    6

    Re: [RESOLVED] WINAPI FindFirstFile & FindNextFile problem

    Here is a sample app:

    Code:
    using System;
    using System.IO;
    using System.Collections.Generic;
    
    namespace DirectoryList
    {
        class Program
        {
            static StreamWriter sw;
            static void Main(string[] args)
            {
                sw = new StreamWriter("dir.txt", false);
                string appdir = Path.GetFullPath(Directory.GetCurrentDirectory());
                ParseDirs(appdir, "");
                sw.Flush();
                sw.Close();
            }
    
            static void ParseDirs(string dir, string leader)
            {
                DirectoryInfo di = new DirectoryInfo(dir);
                DirectoryInfo[] edi = di.GetDirectories();
                FileInfo[] efi = di.GetFiles();
                foreach (DirectoryInfo d in edi) 
                {
                    sw.WriteLine(leader + "<" + d.Name + ">");
                    ParseDirs(dir + @"\" + d.Name, leader + "\t");
                }
                foreach (FileInfo f in efi)
                    sw.WriteLine(leader + f.Name + "  <" + f.Length.ToString("0,0;;0") + " b>");
    
            }
        }
    }
    This code executes 10x slower than the WINAPI version.
    Can this code be optimized somehow?

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

Featured