快速修改文件名

2015-12-19 13:39:44  访问(1145) 赞(0) 踩(0)


相关下载:百度网盘  SlowX.FileNameChangeApp[code]  SlowX.FileNameChangeApp[release]     



  • 所用技术:

    1、ProcessCmdKey - 实现键盘事件

    2、线程定时设置控件焦点

    3、webBrowser - 显示本地路径的图片


    另外:


                // 代码:改成下面的代码 //

                //toolStripComboBox_FileName.Focus();

                //toolStripComboBox_FileName.Select();


                if (!toolStripComboBox_FileName.Focused)

                {

                    toolStripComboBox_FileName.Focus();

                    toolStripComboBox_FileName.Select();

                }


  • 
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void toolStripButton_确定_Click(object sender, EventArgs e)
            {
                try
                {
                    TreeViewDataBind();
                }
                catch (Exception err)
                {
                    MsgForm.MsgException(err);
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            protected void TreeViewDataBind()
            {
                string folderName = comboBox_DirName.Text.Trim();
    
                if (folderName.Length == 0)
                {
                    MessageBox.Show("请选择目录");
                    comboBox_DirName.Focus();
                    comboBox_DirName.Select();
                    return;
                }
    
                DirectoryInfo dirInfo = new DirectoryInfo(folderName);
    
                if (!dirInfo.Exists)
                {
                    MessageBox.Show("目录不存在");
                    comboBox_DirName.Focus();
                    comboBox_DirName.Select();
                    return;
                }
    
                TreeView tv = treeView_Main;
                tv.Nodes.Clear();
    
                TreeViewDataBind8Dir
                    (
                        tv.Nodes, 
                        dirInfo,
                        checkBox_仅图片.Checked,
                        checkBox_包括子目录.Checked
                    );
    
                tv.ExpandAll();
    
                if (tv.Nodes.Count == 0)
                {
                    toolStripLabel_Tip.Text = "没有数据";
                    toolStripLabel_Tip.ForeColor = System.Drawing.Color.Red;
    
                    MessageBox.Show("没有数据");
                }
                else
                {
                    toolStripLabel_Tip.Text = "";
                }
            }
    
          
            /// <summary>
            /// 
            /// </summary>
            /// <param name="tnc"></param>
            /// <param name="dirInfo"></param>
            /// <param name="onlyImage"></param>
            /// <param name="includeSon"></param>
            protected int TreeViewDataBind8Dir
                (
                    TreeNodeCollection tnc,
                    DirectoryInfo dirInfo,
                    bool onlyImage,
                    bool includeSon
                )
            {
                int theResult = 0;
                int itemCount = 0;
    
                TreeNode tn = null;
                
                DirectoryInfo[] dA = dirInfo.GetDirectories();
    
                if (dA != null)
                {
                    foreach (DirectoryInfo dI in dA)
                    {
                        tn = new TreeNode();
                        tn.Text = dI.Name;
                        tn.Tag = dI;
    
    
                        if (includeSon)
                        {
                            itemCount = TreeViewDataBind8Dir
                                (
                                    tn.Nodes, 
                                    dI,
                                    onlyImage,
                                    includeSon
                                );
                        }
    
                        if (itemCount == 0)
                            continue;
    
                        theResult += itemCount;
                        tnc.Add(tn);
                    }
                }
    
                FileInfo[] fA = dirInfo.GetFiles();
    
                if (fA != null)
                {
                    if (onlyImage)
                    {
                        foreach (FileInfo fI in fA)
                        {
                            if (!IsImageFile(fI))
                                continue;
    
                            tn = new TreeNode();
                            tn.Text = fI.Name;
                            tn.Tag = fI;
    
                            tnc.Add(tn);
                            ++theResult;
                        }
                    }
                    else
                    {
                        foreach (FileInfo fI in fA)
                        {
                            tn = new TreeNode();
                            tn.Text = fI.Name;
                            tn.Tag = fI;
    
                            tnc.Add(tn);
                            ++theResult;
                        }
                    }
                }
    
                return theResult;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void toolStripButton_样例_Click(object sender, EventArgs e)
            {
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button_选_Click(object sender, EventArgs e)
            {
    
                try
                {
                    SelectDirName();
                }
                catch (Exception err)
                {
                    MsgForm.MsgException(err);
                }
    
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            protected void SelectDirName()
            {
                FolderBrowserDialog fd = new FolderBrowserDialog();
    
                if (fd.ShowDialog() != DialogResult.OK)
                    return;
    
                comboBox_DirName.Text = fd.SelectedPath;
            }
    
            /// <summary>
            /// 粘贴目录
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void toolStripButton_粘贴目录_Click(object sender, EventArgs e)
            {
                try
                {
                    PasteDirName();
                }
                catch (Exception err)
                {
                    MsgForm.MsgException(err);
                }
    
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            protected void PasteDirName()
            {
                string str = ClipboardDirFileFullNameGet();
    
                if (str != null)
                {
                    comboBox_DirName.Text = str;
                    return;
                }
    
                comboBox_DirName.Text = ClipbordPasteTextGet();
    
            }
    
            /// <summary>
            /// 粘贴操作
            /// </summary>
            /// <returns></returns>
            public string ClipbordPasteTextGet()
            {
                IDataObject data = Clipboard.GetDataObject();
    
                if (data.GetDataPresent(DataFormats.Text))
                {
                    return data.GetData(DataFormats.Text).ToString();
                }
                else
                {
                    return "";
                }
            }
    
    
                    
            /// <summary>
            /// 获得粘贴板中复制的文件的完整路径
            /// </summary>
            protected string ClipboardDirFileFullNameGet()
            {
                StringCollection sc = Clipboard.GetFileDropList();
    
                if (sc == null || sc.Count == 0)
                    return null;
    
                for (int i = 0; i < sc.Count; ++i)
                {
                    if (IsDirectory(sc[i]))
                        return sc[i];
                }
    
                return null;
            }
    
            /// <summary>
            /// 判断是否是目录
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public bool IsDirectory(string str)
            {
                if (str == null || str.Length == 0)
                    return false;
    
                System.IO.FileAttributes fi 
                    =
                    File.GetAttributes(str);
    
                if ((fi & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    return true;
                }
    
                return false;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="fi"></param>
            /// <returns></returns>
            protected bool IsImageFile(FileInfo fi)
            {
                string strEx = fi.Extension;
    
                if (strEx == null)
                    return false;
    
                strEx = strEx.Trim().ToLower();
    
                switch (strEx)
                {
                    case ".bmp":
                    case ".jpg":
                    case ".png":
                    case ".gif":
                    case ".jpeg":
                        return true;
                    default:
                        return false;
                }
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            protected TreeNode selectedNode = null;
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void treeView_Main_AfterSelect(object sender, TreeViewEventArgs e)
            {
                try
                {
                    if (e.Node == null)
                        return;
    
                    if (e.Action != TreeViewAction.ByMouse)
                        return;
    
                    TreeNode tn = e.Node;
    
    
                    treeViewMainAfterSelect(tn);
                }
                catch (Exception err)
                {
                    MsgForm.MsgException(err);
                }
    
    
            }
    
     
    
            /// <summary>
            /// webBrowser - 显示本地路径的图片
            /// </summary>
            /// <param name="bName"></param>
            /// <returns></returns>
            protected string ConvertToImage(string bName)
            {
    
                if (File.Exists(bName))
                    return "<img src=\"file:///" + bName.Replace("\\", "/") + "\" style=\"border:0px;\" alt=\"\" />";
    
                return "";
            }
    
            /// <summary>
            /// webBrowser 设置显示本地图片
            /// </summary>
            /// <param name="str"></param>
            protected void WebBrowserDocumentTextSet(string str)
            {
                webBrowser1.DocumentText = ConvertToImage(str);
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="tn"></param>
            protected void treeViewMainAfterSelect(TreeNode tn)
            {
                FileInfo info = tn.Tag as FileInfo;
    
                if (info == null)
                    return;
    
                selectedNode = tn;
    
    
                if (IsImageFile(info))
                {
                    WebBrowserDocumentTextSet(info.FullName);
                }
    
                string strName = info.Name;
    
                int idx = strName.LastIndexOf('.');
    
                string fileName = null;
                string fileEx = null;
    
                if (idx != -1)
                {
                    fileName = strName.Substring(0, idx);
                    fileEx = strName.Substring(idx + 1);
                }
                else
                {
                    fileName = strName;
                    fileEx = "";
                }
    
                toolStripComboBox_FileName.Text = fileName;
                toolStripComboBox_FileEx.Text = fileEx;
    
                textBox_FileName.Text = info.FullName;
    
                this.Refresh();
    
                toolStripComboBox_FileName.Focus();
                toolStripComboBox_FileName.Select();
    
                SetAutoFocus();
            }
    
            Thread execThread = null;
    
            #region 线程设置焦点
    
            /// <summary>  
            /// 设置状态栏提示文字  
            /// </summary>   
            public void SetAutoFocus()  
            {
                if (gId != 0 && execThread != null &&
                    execThread.ThreadState != ThreadState.Stopped)
                {
                    gId = 0;
                    return;
                }
    
                SetAutoFocusConfig config = new SetAutoFocusConfig();  
                config.SleepTime = 10;   
      
                try  
                {  
      
                    BaseThreadClass bt = new BaseThreadClass();  
      
                    bt.data = config;  
      
                    bt.runName = new BaseThreadClass.ProcessRun(DoAutoFocusThread);
    
                    execThread = new Thread(new ThreadStart(bt.RunProcess));
    
                    execThread.IsBackground = true;
                    execThread.Start();  
      
      
                }  
                catch // (Exception err)  
                {  
                    // ClientBAHelper.instance.ExcuteException(this, err);  
                }  
      
      
            }
    
            int gId = 0;
      
            /// <summary>  
            /// 执行关闭的线程  
            /// </summary>  
            /// <param name="objClass"></param>  
            private void DoAutoFocusThread(object objClass)  
            {  
                try  
                {  
                    SetAutoFocusConfig config   
                        =   
                        objClass as SetAutoFocusConfig;  
      
                    if (config == null)  
                    {  
                        return;  
                    }
    
                    while (true)
                    {
                        System.Threading.Thread.Sleep(100);
    
                        ++gId;
    
                        if (gId >= config.SleepTime)
                            break;
                    }
    
                    ThreadAutoFocus();
                    gId = 0;
                }  
                catch // (Exception err)  
                {  
      
                }  
                finally  
                {  
      
                }  
      
      
            }  
      
      
            /// <summary>  
            ///   
            /// </summary>  
            delegate void ThreadAutoFocusCallback();  
      
      
            /// <summary>  
            ///   
            /// </summary>   
            protected void ThreadAutoFocus()  
            {  
                if (this.InvokeRequired)  
                {  
                    ThreadAutoFocusCallback d 
                        = 
                        new ThreadAutoFocusCallback(ThreadAutoFocus);  
      
                    this.Invoke(d, new object[] {  });
    
                    return;
                }
    
                toolStripComboBox_FileName.Focus();
                toolStripComboBox_FileName.Select();
    
            }
    
            #endregion 线程设置焦点
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void toolStripButton_保存并上一条_Click(object sender, EventArgs e)
            {
    
    
                try
                {
                    SaveAndPreNext(false);
                }
                catch (Exception err)
                {
                    MsgForm.MsgException(err);
                }
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void toolStripButton_保存并下一条_Click(object sender, EventArgs e)
            {
                try
                {
                    SaveAndPreNext(true);
                }
                catch (Exception err)
                {
                    MsgForm.MsgException(err);
                }
    
            } 
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="isNext"></param>
            protected void SaveAndPreNext(bool isNext)
            {
                if (selectedNode == null)
                {
                    MessageBox.Show("请选择文件");
                    return;
                }
    
                FileInfo info = selectedNode.Tag as FileInfo;
    
                if (info == null)
                {
                    MessageBox.Show("请选择文件");
                    return;
                }
    
                if (toolStripComboBox_FileName.Text.Trim().Length == 0)
                {
                    MessageBox.Show("请输入文件名");
                    toolStripComboBox_FileName.Focus();
                    toolStripComboBox_FileName.Select();
                    return;
                }
    
                string fileEx = toolStripComboBox_FileEx.Text.Trim();
    
                if (fileEx.Length != 0)
                    fileEx = "." + fileEx;
    
                string fileName = toolStripComboBox_FileName.Text + fileEx;
                string fullName = info.Directory.FullName + "\\" + fileName;
    
                if (info.Name.ToLower() != fileName.ToLower())
                {
                    if (File.Exists(fullName))
                    {
                        MessageBox.Show("文件已存在");
                        toolStripComboBox_FileName.Focus();
                        toolStripComboBox_FileName.Select();
                        return;
                    }
    
                    File.Move(info.FullName, info.Directory.FullName + "\\" + fileName);
     
                }
    
    
    
                selectedNode.Text = fileName;
                selectedNode.Tag = new FileInfo(fullName);
    
                TreeNode newTn = null;
    
                if (isNext)
                    newTn = TreeNodeGetNext(selectedNode);
                else
                    newTn = TreeNodeGetPre(selectedNode);
    
                if (newTn == null)
                {
                    textBox_FileName.Text = "*没有文件";
        
                    if(isNext)
                        toolStripButton_保存并下一条.Enabled = false;
                    else
                        toolStripButton_保存并上一条.Enabled = false;
    
                    return;
                }
    
    
                toolStripButton_保存并下一条.Enabled = true;
                
                toolStripButton_保存并上一条.Enabled = true;
    
                treeView_Main.SelectedNode = newTn;
                treeViewMainAfterSelect(newTn); 
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="tn"></param>
            /// <returns></returns>
            protected TreeNode TreeNodeGetNext(TreeNode tn)
            {
                if (tn == null)
                    return null;
    
                TreeNode theResult = null;
                TreeNode tnFind = null;
    
    
                theResult = tn.NextNode;
    
                if (theResult == null)
                {
                    return TreeNodeGetNext(tn.Parent);
                }
    
                if (TreeNodeIsFileInfo(theResult))
                    return theResult;
    
                DirectoryInfo di = theResult.Tag as DirectoryInfo;
    
                if (di == null)
                    return null;
    
                tnFind = TreeNodeGetFirstFileInfoNode(theResult);
    
                if (tnFind != null)
                    return tnFind;
    
                return TreeNodeGetNext(theResult );
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="tn"></param>
            /// <returns></returns>
            protected TreeNode TreeNodeGetPre(TreeNode tn)
            {
                if (tn == null)
                    return null;
    
                TreeNode theResult = null;
                TreeNode tnFind = null;
    
    
                theResult = tn.PrevNode;
    
                if (theResult == null)
                {
                    return TreeNodeGetPre(tn.Parent);
                }
    
                if (TreeNodeIsFileInfo(theResult))
                    return theResult;
    
                DirectoryInfo di = theResult.Tag as DirectoryInfo;
    
                if (di == null)
                    return null;
    
                tnFind = TreeNodeGetFirstFileInfoNode(theResult);
    
                if (tnFind != null)
                    return tnFind;
    
                return TreeNodeGetPre(theResult);
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="pNode"></param>
            /// <returns></returns>
            protected TreeNode TreeNodeGetFirstFileInfoNode(TreeNode pNode)
            {
                if (pNode == null)
                    return null;
    
                TreeNode theResult = null;
    
                foreach (TreeNode tn in pNode.Nodes)
                {
                    if (TreeNodeIsFileInfo(tn))
                        return tn;
    
                    theResult = TreeNodeGetFirstFileInfoNode(tn);
    
                    if (theResult != null)
                        return theResult;
                }
    
                return null;
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="tn"></param>
            /// <returns></returns>
            protected bool TreeNodeIsFileInfo(TreeNode tn)
            {
                if (tn == null)
                    return false;
    
                return tn.Tag is FileInfo;
            }
    
            /// <summary>
            /// ProcessCmdKey - 实现键盘事件
            /// </summary>
            /// <param name="msg"></param>
            /// <param name="keyData"></param>
            /// <returns></returns>
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                
                bool isListenCmdKey = false;
    
                if (this.toolStripComboBox_FileName.Focused
                    || this.toolStripComboBox_FileEx.Focused)
                {
                    isListenCmdKey = true;
                }
    
                if (isListenCmdKey)
                {
                    if (keyData == (Keys.Control | Keys.S))
                    {
                        SaveAndPreNext(false);
                        return true;
                    }
    
                    if (keyData == Keys.Enter)
                    {
                        SaveAndPreNext(true);
                        return true;
                    }                
                }
    
                return base.ProcessCmdKey(ref msg, keyData);
            }
     
             
    

标签:快速修改文件名 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)