private
void button2_Click(object sender, EventArgs e)
{
WIN32_FIND_DATA FindFileData1 = new WIN32_FIND_DATA();
var file = "C:\Windows\System32\osk.exe";
//以32位方式编译 找不到文件 。。。 var hFind = FindFirstFile(file, ref FindFileData1);
}
#region 声明WIN32API函数以及结构 **************************************
[Serializable,
System.Runtime.InteropServices.StructLayout
(System.Runtime.InteropServices.LayoutKind.Sequential,
CharSet = System.Runtime.InteropServices.CharSet.Auto
),
System.Runtime.InteropServices.BestFitMapping(false)]
privatestruct WIN32_FIND_DATA
{
publicint dwFileAttributes;
publicint ftCreationTime_dwLowDateTime;
publicint ftCreationTime_dwHighDateTime;
publicint ftLastAccessTime_dwLowDateTime;
publicint ftLastAccessTime_dwHighDateTime;
publicint ftLastWriteTime_dwLowDateTime;
publicint ftLastWriteTime_dwHighDateTime;
publicint nFileSizeHigh;
publicint nFileSizeLow;
publicint dwReserved0;
publicint dwReserved1;
[System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst = 260)]
publicstring cFileName;
[System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByValTStr,
SizeConst = 14)]
publicstring cAlternateFileName;
}
[System.Runtime.InteropServices.DllImport
("kernel32.dll",
CharSet = System.Runtime.InteropServices.CharSet.Auto,
SetLastError = true)]
privatestaticextern IntPtr FindFirstFile(string pFileName, ref WIN32_FIND_DATA pFindFileData);
[System.Runtime.InteropServices.DllImport
("kernel32.dll",
CharSet = System.Runtime.InteropServices.CharSet.Auto,
SetLastError = true)]
privatestaticexternbool FindNextFile(IntPtr hndFindFile, ref WIN32_FIND_DATA lpFindFileData);
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
privatestaticexternbool FindClose(IntPtr hndFindFile);
#endregion//具体方法函数
Stack<string> m_scopes = new Stack<string>();
privatestaticreadonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
WIN32_FIND_DATA FindFileData;
private System.IntPtr hFind = INVALID_HANDLE_VALUE;
void FindFileInDir(string rootDir)
{
string path = rootDir;
start:
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, Path.Combine(path, ".")).Demand();
if (path[path.Length - 1] != ‘\‘)
{
path = path + "\";
}
//Response.Write("文件夹为:" + path + "<br>");
hFind = FindFirstFile(Path.Combine(path, "*"), ref FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (FindFileData.cFileName.Equals(@".") || FindFileData.cFileName.Equals(@".."))
continue;
if ((FindFileData.dwFileAttributes & 0x10) != 0)
{
m_scopes.Push(Path.Combine(path, FindFileData.cFileName));
}
else
{
//Response.Write(FindFileData.cFileName + "<br>"); }
}
while (FindNextFile(hFind, ref FindFileData));
}
FindClose(hFind);
if (m_scopes.Count > 0)
{
path = m_scopes.Pop();
goto start;
}
}
转自 https://www.cnblogs.com/dekevin/p/3926608.html
原文:https://www.cnblogs.com/enych/p/12177128.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!