自制Windows Mobile的文件浏览器
使用Windows 自带的文件浏览器只能察看My Doucument中间的文件,这叫人非常不爽。
于是我自己就做了一个文件浏览器
public partial class FileExplorerDll : UserControl
{
public FileExplorerDll()
{
InitializeComponent();
addNode(this.treeView1);
}
private string selectName;
public string SelectName
{
get
{
return selectName;
}
}
public new void Show()
{
this.Visible = true;
}
private void addNode(TreeView tv)
{
string[] dirNames = Directory.GetDirectories(@"\");
string[] fileNames = Directory.GetFiles(@"\");
int index = 0;
foreach (string dirName in dirNames)
{
tv.Nodes.Add(dirName);
addNode(treeView1.Nodes[index], dirName);
tv.Nodes[index].ImageIndex = 0;
index++;
}
foreach (string fileName in fileNames)
{
tv.Nodes.Add(fileName);
tv.Nodes[index].ImageIndex = 1;
index++;
}
}
private void addNode(TreeNode tn, string path)
{
string[] dirNames;
string[] fileNames;
int index = 0;
if (Directory.Exists(path))
{
dirNames = Directory.GetDirectories(path);
foreach (string dirName in dirNames)
{
tn.Nodes.Add(dirName.Remove(0, path.Length));
addNode(tn.Nodes[index], dirName);
tn.Nodes[index].ImageIndex = 0;
index++;
}
fileNames = Directory.GetFiles(path);
foreach (string fileName in fileNames)
{
tn.Nodes.Add(fileName.Remove(0, path.Length));
tn.Nodes[index].ImageIndex = 1;
index++;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
selectName = this.treeView1.SelectedNode.FullPath;
this.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
selectName = "";
this.Visible = false;
}
}
{
public FileExplorerDll()
{
InitializeComponent();
addNode(this.treeView1);
}
private string selectName;
public string SelectName
{
get
{
return selectName;
}
}
public new void Show()
{
this.Visible = true;
}
private void addNode(TreeView tv)
{
string[] dirNames = Directory.GetDirectories(@"\");
string[] fileNames = Directory.GetFiles(@"\");
int index = 0;
foreach (string dirName in dirNames)
{
tv.Nodes.Add(dirName);
addNode(treeView1.Nodes[index], dirName);
tv.Nodes[index].ImageIndex = 0;
index++;
}
foreach (string fileName in fileNames)
{
tv.Nodes.Add(fileName);
tv.Nodes[index].ImageIndex = 1;
index++;
}
}
private void addNode(TreeNode tn, string path)
{
string[] dirNames;
string[] fileNames;
int index = 0;
if (Directory.Exists(path))
{
dirNames = Directory.GetDirectories(path);
foreach (string dirName in dirNames)
{
tn.Nodes.Add(dirName.Remove(0, path.Length));
addNode(tn.Nodes[index], dirName);
tn.Nodes[index].ImageIndex = 0;
index++;
}
fileNames = Directory.GetFiles(path);
foreach (string fileName in fileNames)
{
tn.Nodes.Add(fileName.Remove(0, path.Length));
tn.Nodes[index].ImageIndex = 1;
index++;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
selectName = this.treeView1.SelectedNode.FullPath;
this.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
selectName = "";
this.Visible = false;
}
}
基本功能实现了,可是加载的速度实在是慢!
还有一个巨大的缺陷,就是用户通过我的这个界面选择的文件地址无法返回,请高手指教,谢谢!