string virtualPath = "~/Content/reports/Product";
string oldFileName = this.Server.MapPath(string.Format(virtualPath + "/{0}", oldfileName)); //完整文件路径
oldFileName : D:\\XX\\Content\\reports\\Product\\AA.doc
string newFileName= this.Server.MapPath(string.Format(virtualPath + "/{0}{1}", proInfo.F_EnCode, FileEextension)); newFileName: D:\\XX\\Content\\reports\\Product\\XX.doc
//FileEextension 后缀名
if (System.IO.File.Exists(oldFileName))
{
if (System.IO.File.Exists(newFileName))
{
string path = Path.GetDirectoryName(newFileName);
DirectoryInfo Folder = new DirectoryInfo(path);
IEnumerable<FileInfo> file = Folder.GetFiles().Where(t => t.FullName == newFileName);
foreach (var item in file)
{
item.Delete(); //删除文件名为newFileName的文件
//item.MoveTo(Path.Combine(destFolder, newFileName)); //destFolder目的文件夹,
//不包含文件 newFileName新的文件名
}
}
System.IO.File.Move(oldFileName, newFileName); //修改文件名为newFileName oldFileName是旧名字