文件名唯一(A.txt => An.txt)

/// <summary>
        /// 文件名唯一(A.txt => An.txt)
        /// </summary>
        /// <param name="fullName">文件名</param>
        /// <returns></returns>
        private string SingleFullName(string fullName)
        {
            string newfullName = fullName;
            FileInfo fileInfo = new FileInfo(newfullName);
            if (fileInfo.Exists)
            {
                int i = 1;
                string fileName = fileInfo.Name;
                string fileExt = fileInfo.Extension;
                int extIndex = fileName.LastIndexOf(fileExt);
                string name = fileName.Substring(0,extIndex);
                newfullName = name + i + fileExt; 
                while (true)
                {
                    fileInfo = new FileInfo(newfullName);
                    if (fileInfo.Exists)
                    {
                        i++;
                        newfullName = name + i + fileExt; 
                    }
                    else
                    {
                        break;
                    }
                }
            }
            
            return newfullName;
        }

 

posted @ 2015-12-21 17:44  慧由心生  阅读(293)  评论(0编辑  收藏  举报