日新阁

取日三省之意,记生活珠玑,每日清新,谓日新阁.

 

两种模式的资源管理器代码之———— 新建文件夹

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;
    }

}


posted on 2007-04-25 14:53  flashicp  阅读(977)  评论(0编辑  收藏  举报

导航