andrew4582

AvailableFileName

Dec 11th, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.48 KB | None | 0 0
  1.         public static string AvailableFileName(string fileName)
  2.         {
  3.             if (!File.Exists(fileName))
  4.             {
  5.                 return fileName;
  6.             }
  7.             string str = Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName));
  8.             string extension = Path.GetExtension(fileName);
  9.             int num = 1;
  10.             while (true)
  11.             {
  12.                 string path = string.Format("{0}({1}){2}", str, num, extension);
  13.                 if (!File.Exists(path))
  14.                 {
  15.                     return path;
  16.                 }
  17.                 num++;
  18.             }
  19.         }
Advertisement
Add Comment
Please, Sign In to add comment