[英]C# File Naming Incremented Name 001++
I am using C# to create a small app. 我正在使用C#创建一个小应用程序。 I need to name files in a recursive manner, incrementing the file names as they are created.
我需要以递归方式命名文件,在创建文件名时递增它们。 I need a file name in the following format : "alt-001.tmp" "alt-002.tmp" and so on.
我需要一个文件名,格式如下:“alt-001.tmp”“alt-002.tmp”等等。 I am always seeing the 00 removed before the incremented number, so for example, 001++ then returns 2 and not 002.
我总是在递增的数字之前看到00被删除,所以例如,001 ++然后返回2而不是002。
Thanks for your help and sorry if it sounds like a stupid question. 如果这听起来像个愚蠢的问题,感谢您的帮助和抱歉。
Use this for format the number as a string: 使用此选项将数字格式化为字符串:
fileName = string.format("alt-{0:000}.tmp", yourCounterVariable);
The string format command, replaces the "{0}" with the variable there. 字符串格式命令用那里的变量替换“{0}”。 Then the values after the column are a mask on how that replacement should be formatted.
然后列之后的值是关于如何格式化替换的掩码。
You need to to a ToString()
on the counter and use a format string. 您需要在计数器上使用
ToString()
并使用格式字符串。
var a = 0;
(a++).ToString("000").Dump();
(a++).ToString("000").Dump();
That will output to 3 digits for you. 这将为您输出3位数。
Results: 001 002 结果:001 002
Something like this noddy example? 像这个点头的例子?
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.