[英]File Rename not working
FileInfo _fileinfo = new FileInfo(@"D:\Projects\LSImage\" + dt.Rows[0]["ApplicationImage"].ToString());
File.Move(_fileinfo.FullName, _fileinfo.FullName.ToString().Replace(_fileinfo.FullName, dt.Rows[0]["ApplicationID"].ToString() + dt.Rows[0]["ApplicationImage"].ToString()));
It's throwing an error 抛出错误
Cannot create a file when that file already exists. 该文件已存在时无法创建该文件。
I have to rename images in LsImages with applicationId from database, so i am prefixing with it application and and then applicationimage name from database 我必须使用数据库中的applicationId重命名LsImages中的图像,所以我要为其添加前缀application,然后是数据库中的applicationimage名称
You are replacing the full string: 您要替换完整的字符串:
File.Move(_fileinfo.FullName, _fileinfo.FullName .ToString().Replace( _fileinfo.FullName , dt.Rows[0]["ApplicationID"].ToString() + dt.Rows[0]["ApplicationImage"].ToString())); File.Move(_fileinfo.FullName, _fileinfo.FullName .ToString()。Replace( _fileinfo.FullName ,dt.Rows [0] [“ ApplicationID”]。ToString()+ dt.Rows [0] [“ ApplicationImage”]。的ToString()));
Are you sure you dont want this instead?: 您确定不想要这个吗?:
File.Move(_fileinfo.FullName, _fileinfo.FullName.ToString().Replace(dt.Rows[0]["ApplicationImage"], dt.Rows[0]["ApplicationID"].ToString() + dt.Rows[0]["ApplicationImage"].ToString()));
Or: 要么:
File.Move(_fileinfo.FullName, _fileinfo.FullName.Replace(_fileinfo.Name, dt.Rows[0]["ApplicationID"].ToString() + dt.Rows[0]["ApplicationImage"].ToString()));
var defaultPath=@"D:\Projects\LSImage";
var appImage=dt.Rows[0]["ApplicationImage"].ToString();
var appId=dt.Rows[0]["ApplicationID"].ToString();
var srcFile=Path.Combine(defaultPath,appImage);
var dstFile=Path.Combine(defaultPath,appId + appImage);
FileInfo _fileinfo = new FileInfo(srcFile); // This isn't needed to rename
File.Move(srcFile,dstFile);
FileInfo _fileinfo = new FileInfo(@"D:\Projects\LSImage\" + dt.Rows[0]["ApplicationImage"].ToString());
try
{
File.Move(_fileinfo.FullName, _fileinfo.FullName.ToString().Replace(_fileinfo.FullName, dt.Rows[0]["ApplicationID"].ToString() + dt.Rows[0]["ApplicationImage"].ToString()));
}
catch(Exception ex)
{
throw new Exception(String.Format("Trying to rename {0} to {1} failed!",_fileinfo.Fullname,dt.Rows[0]["ApplicationID"].ToString() + dt.Rows[0]["ApplicationImage"].ToString()));
}
Then tell us what it reports. 然后告诉我们它的报告。
It looks like you're trying to prepend the ApplicationID onto the front of the file. 看来您正在尝试将ApplicationID放在文件的开头。 If so, try this: 如果是这样,请尝试以下操作:
FileInfo _fileinfo = new FileInfo(@"D:\Projects\LSImage\" + dt.Rows[0]["ApplicationImage"].ToString());
string appID = dt.Rows[0]["ApplicationID"].ToString();
string appImage = dt.Rows[0]["ApplicationImage"].ToString();
string newFullPath = @"D:\Projects\LSImage\" + appImage + appID;
File.Move(_fileinfo.FullName, newFullPath);
File.Move(_fileinfo.FullNmae,_fileinfo.FullName.Replace(_FileInfo.Name,dt.rows[0]["apid"].tostring() + dr.rows[0]["apname"].tosring()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.