【发布时间】:2015-12-11 07:15:54
【问题描述】:
dynamic counter = 1;
string FileNameWithoutExtestion = "";
FileNameWithoutExtestion = file.Split('.')[0];
string FileExtestion = file.Split('.')[1];
while (System.IO.File.Exists(Dir + file))
{
if (true)
{
counter = counter + 1;
if (FileNameWithoutExtestion.EndsWith('_'))
{
file = FileNameWithoutExtestion + counter.ToString() + "." + FileExtestion;
}
else
{
file = FileNameWithoutExtestion + "_" + counter.ToString() + "." + FileExtestion;
}
}
}
if (FileNameWithoutExtestion.EndsWith('_')) //这里发生错误
怎么了?
【问题讨论】:
-
旁注:
if (true)完全无用,删除即可 -
旁注:你可能想要
var counter = 1;不是dynamic counter = 1; -
@Fubo 是正确的,请参阅工作演示 Ideone。还有为什么 dynamic 为什么不 int ?在这里,您只是递增名为 counter 的变量。