110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace URLsearching
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string dir = "C:\\A\\" + textBox1.Text;
if (Directory.Exists(dir))
{
string[] files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
private void button1_Click(object sender, EventArgs e)
{
//这里是查找的文件名
string filename = "填入查找的文件";
//默认路径是根目录.可以让用户选择盘符
string rootPath = "C:\\";
SearchFile(rootPath, filename);
}
private string SearchFile(string path ,string fileName)
{
try
{
//先查找此文件夹有没有需要的查找的文件
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
if (fileName == file)
{
return path;
}
}
//如果次文件夹下没有查到此文件,则查找此文件夹下的文件
string[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs)
{
SearchFile(dir, fileName);
}
//没查找到需要的文件返回空
return null;
}
catch (Exception e)
{
return null;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string dir = "D:\\新建文件夹\\" + textBox1.Text;
if (Directory.Exists(dir))
{
string[] files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
this.richTextBox1.Text += string.Format("{0}\r\n",files[i].ToString());
}
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}