Permalink
Browse files

Create (unique) file name without date

  • Loading branch information...
1 parent f05f3e0 commit c07814b9a9393a6911b42e5e3db39f73dbad5129 @jicho committed Oct 5, 2011
Showing with 21 additions and 12 deletions.
  1. +21 −12 connectors/aspx/filemanager.aspx.cs
@@ -164,7 +164,7 @@ public void SetConfig()
// What do do when the file exists on disk?
// Options are: rename / replace
- ExistingFileOption = "replace";
+ ExistingFileOption = "rename";
}
private string GetInfo(string path, string fullPhysicalPath)
@@ -1418,25 +1418,34 @@ public static string GetPathWithoutFilename(string path, string filename)
/// <summary>
/// Created a Unique filename for the given filename
/// </summary>
- /// <remarks>
- /// Source: http://stackoverflow.com/questions/1078003/c-how-would-you-make-a-unique-filename-by-adding-a-number/1078967#1078967
- /// </remarks>
/// <param name="filename">A full filename, e.g., c:\temp\myfile.tmp</param>
- /// <returns>A filename like c:\temp\myfile-old-01-01-2011-01-01-30.tmp</returns>
+ /// <returns>A filename like c:\temp\myfile-1.tmp</returns>
public string CreateUniqueFilename(string filename)
{
string basename = Path.Combine(
Path.GetDirectoryName(filename),
Path.GetFileNameWithoutExtension(filename)
);
- string uniquefilename = string.Format("{0}-old-{1}{2}",
- basename,
- String.Format("{0:dd-MM-yyyy-HH-mm-ss}", DateTime.Now),
- Path.GetExtension(filename)
- );
-
- return uniquefilename;
+ // look for file name and create a unique one
+ for (int i = 1; ; i++)
+ {
+ string filePath = basename + "-" + i + Path.GetExtension(filename);
+
+ if (!File.Exists(filePath))
+ {
+
+ try
+ {
+ return filePath;
+ }
+ catch (Exception)
+ {
+ continue;
+ }
+ }
+
+ }
}
/// <summary>

0 comments on commit c07814b

Please sign in to comment.