我想知道它为什么不起作用
string filename = optionFileNameFormat; // "{year}-{month}-{day} {name}"
Dictionary<string, string> tagList = new Dictionary<string, string>();
tagList.Add("author",System.Security.Principal.WindowsIdentity.GetCurrent().Name);
tagList.Add("year" , "" + DateTime.Now.Year);
tagList.Add("month", "" + DateTime.Now.Month);
tagList.Add("day" , "" + DateTime.Now.Day);
foreach (var property in tagList)
{
filename.Replace(@"{" + property.Key + @"}", property.Value);
}
Run Code Online (Sandbox Code Playgroud)
我没有任何错误,但我的字符串没有改变.谢谢
Joe*_*orn 12
可能还有其他问题,但是马上跳出来的是该Replace()函数不会改变字符串.相反,它返回一个新字符串.因此,您需要将函数的结果分配回原始函数:
filename = filename.Replace(@"{" + property.Key + @"}", property.Value);
Run Code Online (Sandbox Code Playgroud)