vs.net 2005 搜索文件夹

vs.net 2005 搜索文件夹,第1张

可以用递归实现查找目录下的所有子目录文件

public void FindFile(string dir) //参数为指定的目录

{

//在指定目录及子目录下查找文件,在listBox1中列出子目录及文件

DirectoryInfo Dir=new DirectoryInfo(dir)

try

{

foreach(DirectoryInfo d in Dir.GetDirectories()) //查找子目录

{

FindFile(Dir+d.ToString()+"\\")

listBox1.Items.Add(Dir+d.ToString()+"\\")//listBox1中填加目录名

}

foreach(FileInfo f in Dir.GetFiles("*.*")) //查找文件胡迅慎

{

listBox1.Items.Add(Dir+f.ToString())//listBox1中填加文件名

}

}

catch(Exception e)

{

MessageBox.Show(e.Message)

}

}

调用

private void button1_Click(object sender, System.EventArgs e)

{

string currentdir="F:\\myprogram\\C#\\FileSearch"//搜索的目录

if(currentdir[currentdir.Length-1]!='\\') //非根目昌毁录

currentdir+="\\"

FindFile(currentdir)//调用查找文件函数

}

加上 using System.IO

删除的话就是:

public void DelDir(string path)

{

DirectoryInfo dir = new DirectoryInfo(path)

DirectoryInfo[] dirs = dir.GetDirectories()

for (int i = 0i <dirs.Lengthi++)

Directory.Delete(dirs[i].FullName,true)

}

这是删除目录下所有子目录及文件。。

另外如果是bs结构的话,是裤敬要设目录的权限控制的

你的是IE9吗?如果是的话,把鼠标放局搭到浏览器最上面右键,选择命令栏,点击页面,最下面有耐启个让你用资源管理器打开的按钮。不过以你的情况来看,应该是不能打开的。原因可能是在注册表中缺少ftp文件夹,或者注册表中HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_INTERNET_SHELL_FOLDERS中的explorer.exe值为0,因此系统的资源管理器无法打开,便选择默认浏览器打开了。下载专桐亩拿门打开ftp的软件即可。注意:检查一下你的IE浏览器路径是否C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE,以及默认浏览器是否ie浏览器,是的话复制楼下的注册表信息,保存为文本文档,再将后缀名更改为reg,双击打开即可导入

记的加分!下面是代码,本人原创!

#include <string>

#include <list>

#include <iostream>

#include <Windows.h>

using namespace std

int myDelete(string path)

{

WIN32_FIND_DATA findData

HANDLE hHandle = NULL

string filePathName

string fullPathName

filePathName = path

filePathName += "\\*.*"

list<string>pathList

hHandle = FindFirstFile(filePathName.data(), &findData)

if ( hHandle == INVALID_HANDLE_VALUE )

{

return 1

}

do

{

if ( "." == (string)findData.cFileName || ".." == (string)findData.cFileName )

{

continue

}

fullPathName = path

fullPathName += "\\"

fullPathName += findData.cFileName

if ( findData.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY )

{

myDelete(fullPathName.data())

}

else

{

string str = fullPathName.substr(fullPathName.length() - 3)//截取文件名后3位

if ( str == "jpg" )

{

pathList.push_back(fullPathName)

}

}

} while ( FindNextFile(hHandle, &findData) )

FindClose(hHandle)

list<string>::reverse_iterator rpos

for ( rpos = pathList.rbegin()rpos != pathList.rend()rpos++)

{

cout <<*rpos <<endl

}

return 0

}

int main()

{

string path = "F:\\a"

myDelete(path)

return 0

}

VS2008下编译 运行都没问题!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8209831.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存