码迷,mamicode.com
首页 > Windows程序 > 详细

C# 保存文件如有重名在原名后加(*)

时间:2016-09-14 20:22:52      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:

C#保存文件如有重名加() **(1)

      private string GetNewPathForDupes(string path)
        {
            string directory = Path.GetDirectoryName(path);
            string filename = Path.GetFileNameWithoutExtension(path);
            string extension = Path.GetExtension(path);
            int counter = 1;
            string newFullPath;
            do
            {
                //string newFilename = "{0}({1}).{2}".FormatWith(filename, counter, extension);
                string newFilename = string.Format("{0}({1}).{2}", filename, counter, extension);
                newFullPath = Path.Combine(directory, newFilename);
                counter++;
            } while (System.IO.File.Exists(newFullPath));
            return newFullPath;
        }

 

C# 保存文件如有重名在原名后加(*)

标签:

原文地址:http://www.cnblogs.com/yimeishui/p/5873159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!