-
March 25th, 2011, 07:27 AM
#1
[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();
}
}
}
-
March 25th, 2011, 08:58 AM
#2
Re: WINAPI FindFirstFile & FindNextFile problem
The problem is solved by adding:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
before
struct WIN32_FIND_DATA
Thanks
-
March 26th, 2011, 02:18 PM
#3
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.
-
March 26th, 2011, 06:54 PM
#4
Re: [RESOLVED] WINAPI FindFirstFile & FindNextFile problem
Yes, but it's much slower, especially if I use FileInfo.Length.
-
March 26th, 2011, 08:11 PM
#5
Re: [RESOLVED] WINAPI FindFirstFile & FindNextFile problem
It might have to do with how you've written the C# code.
-
March 27th, 2011, 08:07 AM
#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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|