'How to rename a file if it already exists in directory (ASP.net)
I have seen quite a few file uploads questions online, but it hasnt seemed to work for me. I need to rename my file if it already exists in the directory.
I have even tried MSDN save as/rename method but it doesn't seem to work, maybe I'm missing something. I dont want to delete or override anything guys.
This is what I'm using so far, but it just overwrites the files
string folderName = Server.MapPath("\\Track\\Upload");
string pathString = System.IO.Path.Combine(folderName, SupDDL.SelectedItem.Text.Trim());
System.IO.Directory.CreateDirectory(pathString);
string fileName = QuoteUpload.FileName;
if (!System.IO.File.Exists(pathString))
{
using (System.IO.FileStream fs = System.IO.File.Create(pathString))
{
for (byte i = 0; i < 100; i++)
{
fs.WriteByte(i);
}
QuoteUpload.SaveAs(pathString)}
}
Excuse me if I haven't given enough details, just let me know if you would like more information.
UPDATED
So I have updated it to this, it does the job, but I would like it to automatically increment numbers, instead of just adding the 2 once.
int count = 2;
string fileNameOnly = Path.GetFileNameWithoutExtension(pathString);
string extension = Path.GetExtension(pathString);
string path = Path.GetDirectoryName(pathString);
string newPath = pathString;
if (!System.IO.File.Exists(pathString))
{
QuoteUpload.SaveAs(pathString);
}
else {
string tempName = string.Format("{0}({1})", fileNameOnly, count++);
newFullPath = Path.Combine(path, tempFileName + extension);
QuoteUpload.SaveAs(newPath); }
}
Solution 1:[1]
Try to just calling the save as.
if (!System.IO.File.Exists(pathString))
{
QuoteUpload.SaveAs(pathString);
}
Solution 2:[2]
From what I know, there is now "Rename()" method in c#.
It's not smart to run over 100 bytes, cause you'll never know how big is the upload file..
What you can do is create Help functions:
the first one - will check whether the file exists
the second one - will delete / override the existing file
the third one - will upload the new file
as follows: 1 + 2.
public void DeleteFileIfExists(string BaseDir, string fileName)
{
DirectoryInfo dir = new DirectoryInfo(BaseDir);
foreach (FileInfo fi in dir.GetFiles())
{
string NameWithoutExtenstion = Path.GetFileNameWithoutExtension(fi.Name);
if (NameWithoutExtenstion.Equals(fileName))
{
fi.Delete();
break;
}
}
}
3.
public void uploadFile(HttpPostedFileBase oldfile, string newFile, string Path, HttpServerUtilityBase Server)
{
string path = Path.Combine(Server.MapPath(Path),
Path.GetFileName(oldfile.FileName)); //file.FileName
oldfile.SaveAs(path);
System.IO.File.Copy(path, Server.MapPath(Path) + "/" + newName + ".FileExt");
DeleteFileIfExists(Server.MapPath(Path), Path.GetFileNameWithoutExtension(oldfile.FileName));
}
this following functions is good for any kind of files.
If you have any further question, please ask.
good luck!
Solution 3:[3]
There is no rename function, you can do this by copy file with new name and delete old one.
C# Sample
string strFilePath = Server.MapPath("~/FilesFolder/");
string strOldFileName = "OldFileName.rar";
string strNewFileName = "NewFilName.rar";
if (System.IO.File.Exists(strFilePath + strOldFileName)) {
System.IO.FileInfo FileInfo = new System.IO.FileInfo(strFilePath + strOldFileName);
FileInfo.CopyTo(strFilePath + strNewFileName, true);
FileInfo.Delete();
}
VB Sample
Dim strFilePath As String = Server.MapPath("~/FilesFolder/")
Dim strOldFileName As String = "OldFileName.rar"
Dim strNewFileName As String = "NewFilName.rar"
If IO.File.Exists(strFilePath & strOldFileName) Then
Dim FileInfo As IO.FileInfo = New IO.FileInfo(strFilePath & strOldFileName)
FileInfo.CopyTo(strFilePath & strNewFileName, True)
FileInfo.Delete()
End If
Solution 4:[4]
Before the using you should put a check to rename the file if it exists:
if (File.Exists(pathString))
File.Move(pathString, newFilePath);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yuliam Chandra |
| Solution 2 | Roy Doron |
| Solution 3 | |
| Solution 4 | Eric Lemes |
