因为项目需要一个选择目录的功能,然.NET中在WINCE中folderBrowserDialog组件却不可用,在网上搜了2天都没找到此类可用资源,更搞的是有个资源说是通过导入Shell32.dll的API方式来调用,为引足足浪费半天时间也没找到WINCE平台上的Shell32.dll究竟在何处!不得已,只好自己写个,没想到2个多小时就搞定了,真搞不懂这么简单的资源网上也没有,难道是做嵌入式开的较少?看来自己的工作思路还是有点问题,碰到问题,首先想到google,这也是长年养成的依赖性。好了,废话不说了,简单讲讲思路如下:
参考界面如下:
整个组件分三部分:
1、选择目录的主界面:
需要一个TreeView组件,主要代码如下:
1 private void FrmSelectFolder_Load(objectsender, EventArgs e) 2 { 3 string errstr = ""; 4 try 5 { 6 tvPathList.Nodes.Clear(); 7 8 string currentPath =Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); 9 string rootPath =Directory.GetDirectoryRoot(currentPath); 10 11 TreeNode rootNode = new TreeNode("我的设备"); 12 tvPathList.Nodes.Add(rootNode); 13 14 //写个递归目录中全部文件夹检索出来 15 ScanFolder(rootNode, rootPath); 16 17 rootNode.Expand(); 18 } 19 catch(System.Exception ex) 20 { 21 errstr = "fail to FrmSelectFolder_Load【" + ex.Message + "】"; 22 MessageBox.Show(errstr); 23 } 24 }
1 private void ScanFolder(TreeNode node, stringpath) 2 { 3 string errstr = ""; 4 try 5 { 6 string[] pathList =Directory.GetDirectories(path); 7 8 foreach (string p inpathList) 9 { 10 string strp = p.Replace(path, ""); 11 12 if (strp.Substring(0, 1) == @"\") 13 { 14 strp = strp.Substring(1, strp.Length - 1); 15 } 16 TreeNode treeNode = newTreeNode(strp); 17 treeNode.ImageIndex = SelectImageIndex(strp, false); 18 node.Nodes.Add(treeNode); 19 20 ScanFolder(treeNode, p); 21 } 22 } 23 catch(System.Exception ex) 24 { 25 errstr = "fail to ScanFolder【" + ex.Message + "】"; 26 MessageBox.Show(errstr); 27 } 28 }
2、新建文件夹界面
考虑到新建文件夹已存在的情况会自动加1.
1 private void btnNew_Click(objectsender, EventArgs e) 2 { 3 string errstr = ""; 4 try 5 { 6 TreeNode node =tvPathList.SelectedNode; 7 if (node == null) 8 { 9 MessageBox.Show("未选择目录"); 10 return; 11 } 12 13 FrmNewFolder fnf = newFrmNewFolder(); 14 fnf.Location = new Point((Screen.PrimaryScreen.Bounds.Width - fnf.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - fnf.Height) / 2); 15 if (fnf.ShowDialog() == DialogResult.Cancel) { return; } 16 17 string folderName =fnf.m_folderName; 18 19 //计算当前目录的绝对路径 20 string currentPath = ""; 21 TreeNode currentNode =node; 22 while (currentNode.Parent != null) 23 { 24 currentPath = currentNode.Text + @"\" +currentPath; 25 currentNode =currentNode.Parent; 26 } 27 28 string newCurrentPath = @"\" + currentPath + folderName + @"\"; 29 30 if(Directory.Exists(newCurrentPath)) 31 { 32 if (folderName != "新建文件夹") 33 { 34 MessageBox.Show("文件夹【" + folderName + "】已存在!"); 35 return; 36 } 37 else 38 { 39 //自动增加一个不存在的文件夹目录 40 int index = 1; 41 string newFolderName = String.Format("{0}{1}", folderName, index); 42 while (Directory.Exists(@"\" + currentPath + newFolderName + @"\")) 43 { 44 index++; 45 newFolderName = String.Format("{0}{1}", folderName, index); 46 } 47 folderName =newFolderName; 48 newCurrentPath = @"\" + currentPath + folderName + @"\"; 49 } 50 } 51 52 DirectoryInfo di =Directory.CreateDirectory(newCurrentPath); 53 54 TreeNode treeNode = newTreeNode(folderName); 55 treeNode.ImageIndex = SelectImageIndex(folderName, false); 56 node.Nodes.Insert(0, treeNode); 57 } 58 catch(System.Exception ex) 59 { 60 errstr = "fail to btnNew_Click【" + ex.Message + "】"; 61 MessageBox.Show(errstr); 62 } 63 }
3、调用此选择目录的主界面方法
1 public bool Execute(ref stringerrstr) 2 { 3 bool bRet = false; 4 try 5 { 6 FrmSelectFolder fsf = newFrmSelectFolder(); 7 fsf.Location = new Point((Screen.PrimaryScreen.Bounds.Width - fsf.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - fsf.Height) / 2); 8 if (fsf.ShowDialog() ==DialogResult.OK) 9 { 10 FolderName =fsf.m_selectFoldeName; 11 bRet = true; 12 } 13 else 14 { 15 errstr = "取消选择"; 16 } 17 returnbRet; 18 } 19 catch(System.Exception ex) 20 { 21 errstr = "fail to Execute【" + ex.Message + "】"; 22 returnbRet; 23 } 24 }
或
1 public bool Execute(Control value, ref stringerrstr) 2 { 3 bool bRet = false; 4 try 5 { 6 FrmSelectFolder fsf = newFrmSelectFolder(); 7 fsf.Location = new Point((value.Width - fsf.Width) / 2, (value.Height - fsf.Height) / 2); 8 if (fsf.ShowDialog() ==DialogResult.OK) 9 { 10 FolderName =fsf.m_selectFoldeName; 11 bRet = true; 12 } 13 else 14 { 15 errstr = "取消选择"; 16 } 17 returnbRet; 18 } 19 catch(System.Exception ex) 20 { 21 errstr = "fail to Execute【" + ex.Message + "】"; 22 returnbRet; 23 } 24 }
最后写个DEMO调用:
1 private void button3_Click(objectsender, EventArgs e) 2 { 3 SelectFolder.SelectFolder sf = newSelectFolder.SelectFolder(); 4 if (sf.Execute(this)) 5 { 6 MessageBox.Show(sf.FolderName); 7 } 8 }
效果如下:
完整代码下载:
http://download.csdn.net/detail/laststep/4528434
注:类似Dialg的组件是怎么做的,没搞明白,因此组件对象化效果不好,不能在运行模式下隐藏,哪位知道怎么做的,交流下,谢谢!