粘贴目录 - 如果粘贴板是目录,则返回目录路径值,如果是字符串,则返回字符串

2015-12-19 11:40:02  访问(1870) 赞(0) 踩(0)


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


标签:粘贴目录 - 如果粘贴板是目录,则返回目录路径值,如果是字符串,则返回字符串 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)