프로그램 세상/ASP.NET

소년포비 2009. 10. 24. 02:12

private string GetUniqueFileNameWithPath (string dirPath, string fileN)
{
        string fileName = fileN;

        int indexOfDot = fileName.LastIndexOf (".");
        string strName = fileName.Substring (0, indexOfDot);
        string strExt = fileName.Substring (indexOfDot + 1);

        bool        bExist = true;
        int                fileCount = 0;

        while (bExist)
        {
                if (File.Exists (Path.Combine (dirPath, fileName)))
                {
                        fileCount++;
                        fileName = strName + "(" + fileCount + ")." + strExt;
                }
                else
                {
                        bExist = false;
                }
        }

        return Path.Combine (dirPath, fileName);
}