WINCE中选择目录组件(C#)

摘要:
看来自己的工作思路还是有点问题,碰到问题,首先想到google,这也是长年养成的依赖性。好了,废话不说了,简单讲讲思路如下:参考界面如下:整个组件分三部分:1、选择目录的主界面:需要一个TreeView组件,主要代码如下:FrmSelectFolder_Load1privatevoidFrmSelectFolder_Load2{3stringerrstr="";4try5{6tvPathList.Nodes.Clear();78stringcurrentPath=Path.GetDirectoryName;9stringrootPath=Directory.GetDirectoryRoot;1011TreeNoderootNode=newTreeNode;12tvPathList.Nodes.Add;1314//写个递归目录中全部文件夹检索出来15ScanFolder;1617rootNode.Expand();18}19catch20{21errstr="failtoFrmSelectFolder_Load";22MessageBox.Show;23}24}ScanFolder1privatevoidScanFolder2{3stringerrstr="";4try5{6string[]pathList=Directory.GetDirectories;78foreach9{10stringstrp=p.Replace;1112if13{14strp=strp.Substring;15}16TreeNodetreeNode=newTreeNode;17treeNode.ImageIndex=SelectImageIndex;18node.Nodes.Add;1920ScanFolder;21}22}23catch24{25errstr="failtoScanFolder";26MessageBox.Show;27}28}2、新建文件夹界面考虑到新建文件夹已存在的情况会自动加1.btnNew_Click1privatevoidbtnNew_Click2{3stringerrstr="";4try5{6TreeNodenode=tvPathList.SelectedNode;7if8{9MessageBox.Show;10return;11}1213FrmNewFolderfnf=newFrmNewFolder();14fnf.Location=newPoint;15if{return;}1617stringfolderName=fnf.m_folderName;1819//计算当前目录的绝对路径20stringcurrentPath="";21TreeNodecurrentNode=node;22while(currentNode.Parent!="新建文件夹")33{34MessageBox.Show("文件夹已存在!

因为项目需要一个选择目录的功能,然.NET中在WINCE中folderBrowserDialog组件却不可用,在网上搜了2天都没找到此类可用资源,更搞的是有个资源说是通过导入Shell32.dll的API方式来调用,为引足足浪费半天时间也没找到WINCE平台上的Shell32.dll究竟在何处!不得已,只好自己写个,没想到2个多小时就搞定了,真搞不懂这么简单的资源网上也没有,难道是做嵌入式开的较少?看来自己的工作思路还是有点问题,碰到问题,首先想到google,这也是长年养成的依赖性。好了,废话不说了,简单讲讲思路如下:

参考界面如下:

WINCE中选择目录组件(C#)第1张

整个组件分三部分:

1、选择目录的主界面:

需要一个TreeView组件,主要代码如下:

WINCE中选择目录组件(C#)第2张WINCE中选择目录组件(C#)第3张FrmSelectFolder_Load
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         }
WINCE中选择目录组件(C#)第4张WINCE中选择目录组件(C#)第5张ScanFolder
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.

WINCE中选择目录组件(C#)第6张WINCE中选择目录组件(C#)第7张btnNew_Click
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、调用此选择目录的主界面方法

WINCE中选择目录组件(C#)第8张WINCE中选择目录组件(C#)第9张Execute
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         }

WINCE中选择目录组件(C#)第10张WINCE中选择目录组件(C#)第11张Execute
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调用:

WINCE中选择目录组件(C#)第12张WINCE中选择目录组件(C#)第13张View Code
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         }

效果如下:

WINCE中选择目录组件(C#)第14张

完整代码下载:

http://download.csdn.net/detail/laststep/4528434

注:类似Dialg的组件是怎么做的,没搞明白,因此组件对象化效果不好,不能在运行模式下隐藏,哪位知道怎么做的,交流下,谢谢!

免责声明:文章转载自《WINCE中选择目录组件(C#)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇五、安装Kibana.NET开源报表控件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot Controller接收参数的几种常用方式

第一类:请求路径参数 1. @PathVariable 获取路径参数。即url/{id}这种形式。 2. @RequestParam 获取查询参数。即url?name=这种形式 例子 GEThttp://localhost:8080/demo/123?name=suki_rong对应的java代码 @GetMapping("/demo/{id}") pub...

C# excel 常用操作

1、excel文件读取 1.  com组件操作excel 读写 2.  ado.net方式操作excel 读写 3.  开源的第三方组件npoi 4. open xml 方式读写excel 方式一使用OleDbConnection System.Data.DataTable dt =GetExcelDatatable("C:\Users\Administr...

C# HTTP请求 异步(async await)

static void Main(string[] args) { new Task(() => { Invoke(); }).Start(); Console.WriteLine("我是主线...

commons-pool2 实现 sftp 连接池

简介 ssh 默认的连接数量有限,当大量请求连接 ssh 时会概率性连接失败甚至直接失败,因此需要对连接池化,当然如果不要求实时的话可以用生产者消费者。 了解 commons-pool2 依赖 <dependency> <groupId>org.apache.commons</groupId> <a...

Slf4j MDC机制

转自:  https://www.liangzl.com/get-article-detail-572.html MDC 简介 MDC ( Mapped Diagnostic Contexts ),它是一个线程安全的存放诊断日志的容器。 Logback设计的一个目标之一是对分布式应用系统的审计和调试。在现在的分布式系统中,需要同时处理很多的请求。如何来很好...

vb中从域名得到IP及从IP得到域名

'vb中从域名得到IP及从IP得到域名 Private Const WS_VERSION_REQD = &H101 Private Const WS_VERSION_MAJOR = WS_VERSION_REQD &H100 And &HFF& Private Const WS_VERSION_MINOR =...