开发者

File name change if exists in a folder in C#

In my application I need to save a file(image,pdf,txt) to a folder. I nee开发者_StackOverflow中文版d to add as many images or files to my folder. Suppose I have an image with name "image1", if this already exists in the folder and if another user trying to add another image with the name "image1" then my application automatically needs to change imagename to "image2". So the application should check whether the file exist, if so save the new image with a different name. Any help will be appreciated.


Use the File.Exists method to determine the presence of a file.

However, it must be noted that conflict could still occur. For instance, there is a race condition in that the file could potentially be saved by someone else after the call to determine existence, yet before your call to create the file, so you will still need to account for error.

What you might consider is giving each saved (uploaded?) file a unique name, and referencing them in a database - you can use Guid.NewGuid quite reliably in this case.

As for actually saving the file, you have numerous options here: you could use a FileStream, static methods exposed by the File class, or method of the FileUpload control if you're using that. Please clarify your circumstances.

Lastly, depending on there size, and if a database is being used anyway, you might want to consider storing them as binary data in there, then name conflicts may be irrelevant.


Of course, you can check if the file exists with System.IO.File.Exists(...), but I think your requirements might be a bit too optimistic... there are multiple cases which you should consider. Suppose that you have:

upload.extension
upload1.extension
upload01.extension
upload001.extension

The names above suggest that you should have some naming convention, but that naming convention is not going to be universal (unless you want to have a bunch of naming conventions to cover all of those cases). If the user wants to save a file named "upload" and your naming convention states that the file name would be incremented with a digit with no leading numbers, then you would try "upload1" and if that's not available then "upload2" until you find one that's available.

Let's take the simple case with the convention of adding a digit with no leading zeroes:

int i = 0
string fileExtension = ".extension";
string availableFileName = fileName;
while(System.IO.File.Exists(availableFileName+fileExtension))
{
    availableFileName = fileName + i;
    i++;
}
fileUpload.SaveAs(availableFileName+fileExtension);

This would append an integer at the end of the file name until you find a file name that is not duplicated.


You can do like...

 if (System.IO.File.Exists("Path"))
    {
        fileUpload1.SaveAs("Path + New FileName");
    }

However it would be better if you save the file with appending Current DataTime with the filename. e.g.

fileUpload1.SaveAs("Path + Orginal FileName" + DateTime.Now.ToString("yyyy-MM-dd HHmmtt") + "File Extension";


You should be able to check if the file name already exists using System.IO.File.Exists...

if(System.IO.File.Exists("image1")){
  //Use a different name
}

Of course you would need to refine this example to be more flexible for your specific needs.


You can check for a file's existence with the System.IO.File.Exists[MSDN] method. It takes a path string as its argument.

For manipulating the path string should a file exist, take a look at System.IO.Path[MSDN]. It's a great utility for doing just what you need.


I've successfully solved this by having any uploaded files be named by GUID's string representation.

If needed, you can maintain the mapping between GUID's generated file name and the original filename in the DB.

Or just use this:

    // Create a temporary file name to use for checking duplicates.
    string tempfileName = "";

    // Check to see if a file already exists with the
    // same name as the file to upload.        
    if (System.IO.File.Exists(pathToCheck)) 
    {
      int counter = 2;
      while (System.IO.File.Exists(pathToCheck))
      {
        // if a file with this name already exists,
        // prefix the filename with a number.
        tempfileName = counter.ToString() + fileName;
        pathToCheck = savePath + tempfileName;
        counter ++;
      }

      fileName = tempfileName;
}

Source: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜