首页 > Windows开发 > 详细

转载:C#保存文件时重名自动生成新文件的方法

时间:2014-10-17 01:16:34      阅读:346      评论:0      收藏:0      [点我收藏+]
/// <summary>
/// Generates a new path for duplicate filenames.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
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 );
    newFullPath = Path.Combine( directory, newFilename );
    counter++;
    } while ( System.IO.File.Exists( newFullPath ) );
 
    return newFullPath;
}

源地址:http://www.oschina.net/code/snippet_4873_1460

 

转载:C#保存文件时重名自动生成新文件的方法

原文:http://www.cnblogs.com/crazymonk/p/4030033.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!