博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

FotoVision对相册文件夹的重命名

Posted on 2007-11-08 14:48  a-peng  阅读(666)  评论(0编辑  收藏  举报
FotoVision对相册的管理在AlbumsPane.cs中实现

AlbumsPane中放入一个ListView控件 用来显示所有相册名称与所含照片数
并对这个ListView提供很好的支持让其功能丰富 包括 文件与文件夹的拖文等.这里只介绍对相册名称的重命名

首先要让ListView支持编辑重命名
private void InitializeComponent()
{
  
this.listView = new System.Windows.Forms.ListView();

  
//支持编辑
  this.listView.LabelEdit = true;

  
//编辑前
  this.listView.BeforeLabelEdit += new System.Windows.Forms.LabelEditEventHandler(this.listView_BeforeLableEdit);

  
//编辑后
  this.listView.AfterLabelEdit += new System.Windows.Forms.LabelEditEventHandler(this.listView_AfterLableEdit);
}

先介绍下LabelEditEventArgs e

       Label (你编辑后的文本如果为空则为null)

       Item (int你编辑项所在的索引号)

       CancelEdit(bool型撤消编辑) e.CancelEdit = true


private bool _inLableEdit = false;

//上下文菜单激活编辑
private void menuRename_Click(object sender, EventArgs e)
{
  
//正处于编辑状态 退出
  if (_inLableEdit)
        
return;

  
if (listView.SelectedItems.Count == 1)
  
{
      listView.SelectedItems[
0].BeginEdit();
  }

}


//编辑之前
private void listView_BeforeLableEdit(object sender, LabelEditEventArgs e)
{
   _inLableEdit 
= true;
}


//编辑之后
private void listView_AfterLableEdit(object sender, LabelEditEventArgs e)
{
   _inLableEdit 
= false;

   
//输入为空
   if (e.Label == null)
   
{
      e.CancelEdit 
= true;
      
return;
   }


   
string oldName = listView.Items[e.Item].Text;
   
string newName = e.Label;

   
//字符串合法
   if (!FileManager.IsValidAlbumName(newName))
   
{
      e.CancelEdit 
= true;
      
return;
   }


   
//验证相册是否存在
   if (FileManager.AlbumExists(newName))
   
{
      MessageBox.Show(
         
this.TopLevelControl, 
         String.Format(
"相册 {0} 已存在,请更换相册名称", newName), 
         
"不能修改相册名"
         MessageBoxButtons.OK, 
         MessageBoxIcon.Exclamation);

      e.CancelEdit 
= true;
      
return;
   }


   
//重命名相册
   if (!RenameAlbum(oldName, newName))
   
{
      e.CancelEdit 
= true;
   }

}


//重命名相册
private bool RenameAlbum(string oldName, string newName)
{
    
try
    
{
       
if (oldName != newName)
       
{
           FileManager.RenameAlbum(oldName, newName);
       }

       
return true;
    }

    
catch
    
{
       
return false;
    }

}

//字符串合法
if (!FileManager.IsValidAlbumName(newName)){}
这里首先涉及到文件夹名称的有效性问题

FotoVision中规定 相册名称最大长度100
文件夹名称不能有 \ / : * ? "  < > | 这些字符出现 且文件夹名开头与结尾不能有 (空格) 与 (点号)
接着就是同一目录中不能有相同名称的文件夹

//Class FileManager

private class Consts
{
  
//相册最大长度100 文件夹名称不能有 \ / : * ? "  < > | 这些字符出现
   public const int MaxAlbumLength = 100;
   
public const string InvalidAlbumPattern = "[\\/:*?\"<>|]";
}


//相册名合法性验证
public static bool IsValidAlbumName(string albumName)
{
    
if (albumName.Length == 0 || albumName.Length > Consts.MaxAlbumLength)
        
return false;

    
//文件夹不能以空格开头结尾
    if (albumName.StartsWith(" "|| albumName.EndsWith(" "))
        
return false;

    
//文件夹不能以点开头或结尾
    if (albumName.StartsWith("."|| albumName.EndsWith("."))
        
return false;

    
if (System.Text.RegularExpressions.Regex.IsMatch(albumName, Consts.InvalidAlbumPattern))
         
return false;

    
return true;
}


//相册是否存在
public static bool AlbumExists(string albumName)
{
    
string albumPath = GetLocation(albumName);

    
return Directory.Exists(albumPath);
}


//重命名相册
public static void RenameAlbum(string oldName, string newName)
{
   
string oldAlbumPath = GetLocation(oldName);
   
string newAlbumPath = GetLocation(newName);

   Directory.Move(oldAlbumPath, newAlbumPath);
//将旧文件夹中的内容都移动到新文件夹中 实现重命名
}


重命名结束