C#完成保留文件時重名主動生成新文件的辦法。本站提示廣大學習愛好者:(C#完成保留文件時重名主動生成新文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成保留文件時重名主動生成新文件的辦法正文
本文實例講述了C#完成保留文件時重名主動生成新文件的辦法。分享給年夜家供年夜家參考。詳細以下:
將一個文檔保留為 a.txt 時,發明此文件曾經存在,則主動保留為 a(1).txt
/// <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;
}
願望本文所述對年夜家的C#法式設計有所贊助。