zoukankan      html  css  js  c++  java
  • 两种模式的资源管理器代码之———— 新建文件夹

    1)简洁 简单的代码:

        /***新建文件夹***/

    /***加子节点***/


       private void _tree_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
            
    {
                TreeNode node 
    = this._tree.SelectedNode;
                
                
    if(IsNewDir)
                
    {
         DirectoryInfo dirObj 
    = this.GetDirObj(node.Parent);
                    
    try
                    
    {
                        
    if(e.Label == null)
               
                            dirObj.CreateSubdirectory(e.Node.Text);
                       
                        
    else
                            dirObj.CreateSubdirectory(e.Label);
                    }

                    
    catch(Exception ex)
                    
    {
                        MessageBox.Show(ex.Message);
                    }

                    
    finally
                    
    {
                        IsNewDir 
    = false;
                    }

                }

             }

            

    2)
    在NetDisk下DownLoad.cs中新建文件夹的部分代码:
    如果新建文件夹已经存在,则,给新建文件夹编号。检查文件夹命名是否合法,长度 等 。
    FoloderControl 是目录操作类。this.pre_Path为程序运行路径。

            
    //新建
            private void menuItem1_Click(object sender, System.EventArgs e)
            
    {
         
    string foldername="新建文件夹";
                GetFolderName(foldername);
                
    if(!flagNew)
                
    {
                    foldername
    ="新建文件夹";
                }
     
                
    else
                
    {
                    
    if(this.returnNameId!=-1)
                    
    {
                        foldername
    ="新建文件夹 ("+this.returnNameId.ToString()+")";
                    }

                }


              
    string fpath = this.pre_Path + this.tv_Path.SelectedNode.FullPath + @"\"+foldername;
              TreeNode newNode 
    =new TreeNode(foldername);

                
    try 
                
    {
                    
                    
    this.tv_Path.SelectedNode.Nodes.Add(newNode);
                    
    this.tv_Path.SelectedNode = newNode;
                    oldFolder
    =this.tv_Path.SelectedNode.Text;


                    
    this.tv_Path.LabelEdit = true;
                    
    if (!newNode.IsEditing)
                        newNode.BeginEdit();
                    
                }

                
    catch (Exception err) 
                
    {
                    MessageBox.Show(
    this, err.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                oldFolder
    =this.tv_Path.SelectedNode.Text;
            
            }



        private void  GetFolderName(string name)
            
    {
                
            ArrayList arr
    =new ArrayList();
                
                
    string tempName=name;
                
    string rootPath=this.pre_Path + this.tv_Path.SelectedNode.FullPath;
                DirectoryInfo di 
    = new DirectoryInfo(rootPath);
                DirectoryInfo[] AllSubDir 
    = di.GetDirectories();
                
                
    for(int i=0;i<AllSubDir.Length;i++)
                
    {  

                    
    int index1=AllSubDir[i].Name.IndexOf(name);
                    
    if(index1>=0)
                    
    {
                        tempName
    =AllSubDir[i].Name.Substring(index1+name.Length);
                        tempName
    =tempName.Trim();
                        
    if(tempName!="")
                        
    {
                            tempName
    =tempName.Substring(1,tempName.Length-2);
                            arr.Add(tempName);
                            tempName
    =string.Empty;
                        }

                        
    else
                        
    {
              
    // 有"新建文件夹"
                            flagNew=true;
              
    this.returnNameId=2;
                        }

                    }

                    
    else
                    
    {
                        tempName
    =string.Empty;
                    }

                    
                             
                }


                CheckArrayList(arr)    ;
            }


    private void tv_Path_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
    {
        newFolder
    =e.Node.Text.Trim();
        
    try
        
    {
                
            
    if (e.Label != null)
            
    {
                newFolder
    =e.Label;
                
    if (e.Label.Length > 0)
                
    {
                        
                    
    string fpath =this.pre_Path+ this.tv_Path.SelectedNode.Parent.FullPath + @"\" + e.Label.Trim();

                    
    if (!NameAvaiCheck(e,e.Label.Trim())) 
                        
    // 名称合法
                    {
                        
    if (!Directory.Exists(fpath)) 
                        
    {
                            
    string oldpath=this.pre_Path + this.tv_Path.SelectedNode.Parent.FullPath + @"\"+oldFolder;
                
                            
    if(!Directory.Exists(oldpath)) 
                            
    {

                                FoloderControl.DirectCreat(fpath); 
                            }

                            
    else
                            
    {
                                FoloderControl.DirectChange(fpath,oldpath);
                            }

                            e.Node.EndEdit(
    true);
                            
    this.txtPath.Text=fpath;
                        }

                        
    else
                        
    {
                            e.CancelEdit 
    = true;
                            MessageBox.Show(
    this,"文件夹名已存在。","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            
    this.tv_Path.LabelEdit = true;
                            e.Node.BeginEdit();
                        }

                    }

                    
                }

                
    else
                
    {
                    e.CancelEdit 
    = true;
                    MessageBox.Show(
    this,"文件夹名不能为空。","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    
    this.tv_Path.LabelEdit = true;
                    e.Node.BeginEdit();
                }

            }

            
    else   //e.Label==null
            {
                
    string creatfpath=this.pre_Path + this.tv_Path.SelectedNode.FullPath;

                
    if(!Directory.Exists(creatfpath)) 
                
    {
                    FoloderControl.DirectCreat(creatfpath); 
                }

                
    else
                
    {
                    e.CancelEdit 
    = true;
                }

                           
            }

        }

        
    catch (Exception err)
        
    {
            e.Node.EndEdit(
    true);
            MessageBox.Show(
    this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);

        }

        
    finally
        
    {
    //                this.tv_Path.LabelEdit = false;
        }

    }


  • 相关阅读:
    cookie和session的区别和用法
    JavaScript深浅拷贝
    前端知识
    typescript -- ts
    vue笔记精华部分
    宜人贷项目里-----正则匹配input输入月份规则
    PHP-Socket-阻塞与非阻塞,同步与异步概念的理解
    PHP socket客户端长连接
    PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令
    查看局域网内所有ip
  • 原文地址:https://www.cnblogs.com/flashicp/p/726839.html
Copyright © 2011-2022 走看看