Hi namojainashis...,
Check this example. Now please take its reference and correct your code.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
string[] filesName = { "ABCD,Test.pdf", "ABCD,BBCDFPT,Test.Docx" };
// Using File Copy Method.
foreach (var fileName in filesName)
{
string oldFile = Server.MapPath("~/Files/" + fileName);
string newFile = Server.MapPath("~/Files/" + fileName.Replace(",", ""));
if (System.IO.File.Exists(oldFile))
{
System.IO.File.Copy(oldFile, newFile, true);
System.IO.File.Delete(oldFile);
}
}
// Using FileInfo MoveTo Method.
foreach (var fileName in filesName)
{
string oldFile = Server.MapPath("~/Files/" + fileName);
string newFile = Server.MapPath("~/Files/" + fileName.Replace(",", ""));
System.IO.FileInfo fi = new System.IO.FileInfo(oldFile);
if (fi.Exists)
{
fi.MoveTo(newFile);
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim filesName As String() = {"ABCD,Test.pdf", "ABCD,BBCDFPT,Test.Docx"}
' Using File Copy Method.
For Each fileName In filesName
Dim oldFile As String = Server.MapPath("~/Files/" & fileName)
Dim newFile As String = Server.MapPath("~/Files/" & fileName.Replace(",", ""))
If IO.File.Exists(oldFile) Then
IO.File.Copy(oldFile, newFile, True)
IO.File.Delete(oldFile)
End If
Next
' Using FileInfo MoveTo Method.
For Each fileName In filesName
Dim oldFile As String = Server.MapPath("~/Files/" & fileName)
Dim newFile As String = Server.MapPath("~/Files/" & fileName.Replace(",", ""))
Dim fi As IO.FileInfo = New IO.FileInfo(oldFile)
If fi.Exists Then
fi.MoveTo(newFile)
End If
Next
End Sub