using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
static uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
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;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll")]
public static extern bool FindClose(IntPtr hFindFile);
static void Main(string[] args)
{
foreach (var f in enumfiles("C:\\"))
{
Console.WriteLine(f.cFileName);
}
}
static IEnumerable<WIN32_FIND_DATA> enumfiles(string dirname)
{
var stack = new Stack<string>();
stack.Push(dirname);
while (stack.Count > 0)
{
var present = stack.Pop();
WIN32_FIND_DATA win32fd;
IntPtr find = FindFirstFile(Path.Combine(present, "*"), out win32fd);
if (find == INVALID_HANDLE_VALUE)
{
continue;
}
do
{
if ((win32fd.cFileName == ".") || (win32fd.cFileName == ".."))
{
continue;
}
win32fd.cFileName = Path.Combine(present, win32fd.cFileName);
if ((win32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
yield return win32fd;
}
else
{
stack.Push(win32fd.cFileName);
}
} while (FindNextFile(find, out win32fd));
FindClose(find);
}
}
}
}