C#實(shí)現(xiàn)保存文件時(shí)重名自動(dòng)生成新文件的方法
更新時(shí)間:2015年07月27日 10:52:20 作者:華宰
這篇文章主要介紹了C#實(shí)現(xiàn)保存文件時(shí)重名自動(dòng)生成新文件的方法,涉及C#針對(duì)保存文件時(shí)出現(xiàn)重命名情況的自動(dòng)處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)保存文件時(shí)重名自動(dòng)生成新文件的方法。分享給大家供大家參考。具體如下:
將一個(gè)文檔保存為 a.txt 時(shí),發(fā)現(xiàn)此文件已經(jīng)存在,則自動(dòng)保存為 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;
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
相關(guān)文章
C#基礎(chǔ):Equals()與運(yùn)算符==的區(qū)別分析
本篇文章是對(duì)c#中的Equals()與運(yùn)算符==的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#判斷指定驅(qū)動(dòng)器是否已經(jīng)準(zhǔn)備就緒的方法
這篇文章主要介紹了C#判斷指定驅(qū)動(dòng)器是否已經(jīng)準(zhǔn)備就緒的方法,涉及C#針對(duì)硬件IO操作的技巧,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)用于操作wav聲音文件的類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)用于操作wav聲音文件的類,實(shí)例分析了C#操作wav音頻文件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

