读取指定目录下所有文件夹和文件的路径

mrpmc 2014-08-20 03:06:18
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Security.AccessControl;

namespace 文檔路徑讀取
{
public partial class Form1 : Form
{
public static string url;
public Form1()
{
InitializeComponent();
read();
}

private void button1_Click(object sender, EventArgs e)//選擇文件夾
{
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
folderBrowserDialog1.Description = "請選擇文件夾";
folderBrowserDialog1.ShowNewFolderButton = false;
DialogResult d = folderBrowserDialog1.ShowDialog();
if (d == DialogResult.OK)
{
url = folderBrowserDialog1.SelectedPath;
label1.Text = folderBrowserDialog1.SelectedPath;
label1.Visible = true;
button2.Visible = true;
}
else
label1.Text = "請選擇目錄!";
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)//开启子线程读取文件夹下所有文件夹和文件路径
{

listBox1.Items.Clear();
this.label5.Text = "0";
ThreadWithState tws = new ThreadWithState(this);
Thread t = new Thread(new ThreadStart(tws.check));
t.Start();
}

public void AddItems(string text)//把路径添加到listbox中
{
if (this.listBox1.InvokeRequired)
{
Action a = () => AddItems(text);
this.listBox1.BeginInvoke(a);
}
else
{
this.listBox1.Items.Add(text);
}
}

public void SetCount()//统计文件夹和文件数量
{
if (this.label5.InvokeRequired)
{
Action a = () => this.label5.Text = this.listBox1.Items.Count.ToString();
this.label5.BeginInvoke(a);
}
else
{
this.label5.Text = this.listBox1.Items.Count.ToString(); ;
}
}

public void save()//把listbox的内容保存到当前程序目录下的save.txt文档
{
if (this.InvokeRequired)
{
Action a = () => save();
this.BeginInvoke(a);
}
else
{
string s = "";
StringBuilder saveString = new StringBuilder();
for (int i = 0; i < this.listBox1.Items.Count; i++)
{
s = this.listBox1.Items[i].ToString();;
saveString = saveString.Append(s).Append("}split{");
}
saveString = saveString.Remove(saveString.Length - 7, 7);//删除最后一个"}split{"
File.WriteAllText(Application.StartupPath + "\\save.txt", saveString.ToString());
}
}

public void read()//读取save.txt内容
{
try
{
string ReadString = "";
ReadString = File.ReadAllText(Application.StartupPath + "\\save.txt");
listBox1.Items.AddRange(ReadString.Split(new string[] { "}split{" }, StringSplitOptions.None));
label5.Text = listBox1.Items.Count.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}
}

//ThreadWithState 类里包含了将要执行的任务以及执行任务的方法
public class ThreadWithState
{
Form1 form;//= new Form1();
public bool Finish;
public ThreadWithState(Form1 form)
{
this.form = form;
}
public void check()
{
try
{
getAllFiles(Form1.url);
form.save();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}

}

public void getAllFiles(string directory) //获取指定的目录中的所有文件(包括文件夹)
{
try
{
getFiles(directory);//获取指定的目录中的所有文件(不包括文件夹)
getDirectory(directory);//获取指定的目录中的所有目录(文件夹)
form.SetCount();// form.label5.Text = form.listBox1.Items.Count.ToString();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

public void getFiles(string directory) //获取指定的目录中的所有文件(不包括文件夹)
{
string[] path = System.IO.Directory.GetFiles(directory);
for (int i = 0; i < path.Length; i++)
{
try
{
form.AddItems(path[i]);// form.listBox1.Items.Add(path[i]);;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}

public void getDirectory(string directory) //获取指定的目录中的所有目录(文件夹)
{
string[] directorys = System.IO.Directory.GetDirectories(directory);
if (directorys.Length <= 0) //如果该目录中没有其他文件夹
form.AddItems( directory);// form.listBox1.Items.Add(directory);
else
{
for (int i = 0; i < directorys.Length; i++)
{
try
{
getAllFiles(directorys[i]);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}
}

读取一写文件夹路径没有任何问题,但读取我电脑上面E盘,第一次可以成功读取E盘下所有文件夹和文件的路径并可以保存,但再读E盘的文件夹,可以读取到所有文件和文件夹路径到listbox,但没有办法保存到save.txt文档,必须重启电脑才能保存。求解!!!万分感激。
...全文
232 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mrpmc 2014-08-23
  • 打赏
  • 举报
回复
工程放在D盘
wind_cloud2011 2014-08-23
  • 打赏
  • 举报
回复
经测试,代码没问题,在你的机子中有没有可能有杀毒软件在监控文件,你在写save.txt前加个判断,是不是被使用中?
於黾 2014-08-21
  • 打赏
  • 举报
回复
那是因为你的工程本身放在E盘了吧?
xrxbl_2694090319 2014-08-21
  • 打赏
  • 举报
回复
获取文件目录路径及名称 protected void Page_Load(object sender, EventArgs e) { string pathName = Server.MapPath("~/App_Data");//取App_Data物理路径 DirectoryInfo dir = new DirectoryInfo(pathName);//创建DirectoryInfo实例 Response.Write("文件夹名称:" + dir.Name + "<br/>");//CodeGo.net/ Response.Write("文件夹目录及路径:" + dir.FullName + "<br/>"); Response.Write("文件夹创建日期:" + dir.CreationTime + "<br/>"); Response.Write("上次访问时间:" + dir.LastAccessTime + "<br/>"); Response.Write("最近修改时间:" + dir.LastWriteTime + "<br/>"); }
mrpmc 2014-08-20
  • 打赏
  • 举报
回复
在另外一台电脑反复测试也没有出现这种情况,求高手指出问题所在。
mrpmc 2014-08-20
  • 打赏
  • 举报
回复
但我读E盘里面的文件夹没有问题,读其他盘遇到没有访问权限的文件会抛出异常,到也可以读出路径,也能保存。唯独E盘不行。关闭程序重开也不行,只有重启电脑才行。
nikolaichow 2014-08-20
  • 打赏
  • 举报
回复
save.txt 在filestream读取后为独占状态,需要释放。
mrpmc 2014-08-20
  • 打赏
  • 举报
回复
我读取E盘里面某个文件夹没有任何问题,可以读取到这个文件夹里面所有文件夹和文件的路径,并可以保存到save.txt。读取整个E盘就不行,也没有抛出任何异常。
wangnaisheng 2014-08-20
  • 打赏
  • 举报
回复
应该是你读没有有释放资源吧,所以无法写。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧