问题描述
我想用新文件夹替换现有文件路径。
I want replace existing file path with new folder.
例如,
ROOTPATH = @" D:\原始" ;;
ROOTPATH = @"D:\Original";
filepath = @" D:\Original\cdm \ cdm \ data \ cdm_bak.mdb";
filepath = @"D:\Original\cdm\cdm\data\cdm_bak.mdb";
BACKUP_ROOTPATH = @" F:\ backup"
BACKUP_ROOTPATH = @"F:\backup"
我想用文件路径中的BACKUP_ROOTPATH替换ROOTPATH来获取new_filepath;
I want to replace ROOTPATH with BACKUP_ROOTPATH from filepath to get new_filepath;
我试过了如下所示;
public static string GetBackupPath(string file)
{
string backupfilepath = "";
string tempstr = "";
tempstr = file.Replace(ROOTPATH, "");
backupfilepath = Path.Combine(BACKUP_ROOTPATH, tempstr);
return (backupfilepath);
}
如果我运行它,tempstr将成为@" \cdm \ cdm \ data \ cdm_bak.mdb"。
If I run this, tempstr become @"\cdm\cdm\data\cdm_bak.mdb".
当它在Path.Combine(BACKUO_ROOT,tempstr)中合并时,它返回@" \cdm\cdm\data\cdm_bak.mdb",而不是@" F:\ backup \cdm\cdm\data\cdm_bak.mdb"。
When it is combined in Path.Combine(BACKUO_ROOT, tempstr), it returns @"\cdm\cdm\data\cdm_bak.mdb", not @"F:\backup\cdm\cdm\data\cdm_bak.mdb".
如果filepath = @" C:\ cdm \ cdm \ data \ cdm_bak.mdb" ;,GetBackupPath返回正确的路径,但不是以及 filepath = @" D:\Original\cdm\cdm\data\cdm_bak.mdb"。
In case filepath =@"C:\cdm\cdm\data\cdm_bak.mdb", GetBackupPath returns correct path but not in case filepath =@"D:\Original\cdm\cdm\data\cdm_bak.mdb".
怎么做无论如何我都能使用吗?
How do I make it work in any case?
推荐答案
尝试使用以下代码,看看这是否是一个解决方案;
Try using the following code and see if this is a solution;
filepath = filepath.Replace( ROOTPATH, BACKUP_ROOTPATH );
希望这会有所帮助:)
Hope this helps :)
这篇关于替换&文件夹名称的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!