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

/// <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#保存文件时重名自动生成新文件的方法

上一篇:BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--客户端对象模型和REST APIs概览 Windows Phone


下一篇:3. 线性回归