C#文件命名递增的名称001 ++

| 我正在使用C#创建一个小型应用程序。我需要以递归方式命名文件,并在创建文件时增加文件名。我需要以下格式的文件名:\“ alt-001.tmp \” \“ alt-002.tmp \”,依此类推。我总是看到递增的数字之前删除了00,因此,例如001 ++然后返回2而不是002。 感谢您的帮助,如果听起来像个愚蠢的问题,请对不起。
已邀请:
使用此命令将数字格式化为字符串:
fileName = string.format(\"alt-{0:000}.tmp\", yourCounterVariable);
字符串格式命令,用那里的变量替换\“ {0} \”。然后,该列之后的值是有关应如何设置替换格式的掩码。
您需要在计数器上加1,并使用格式字符串。
var a = 0;
(a++).ToString(\"000\").Dump();
(a++).ToString(\"000\").Dump();
这将为您输出3位数字。 结果: 001 002
像这个点头例子?
int unique = 0;
string destPath = string.Format(\"alt-{0:000}.tmp\", unique);
while (File.Exists(destPath))
{
     unique++;
     destPath = Path.Combine(easyPath, string.Concat(baseName, \" \", unique.ToString(\"00\", CultureInfo.InvariantCulture), file.Extension));
}

要回复问题请先登录注册