[英]Why i'm getting NotSupportedException: The given path's format is not supported?
我创建了这个方法:
private string GetDirectoryName(string baseString, string baseName, int startAt)
{
int i = startAt;
while (Directory.Exists(Path.Combine(baseString, baseName + i.ToString("D6")+"_"+DateTime.Now)))
{
i++;
}
return Path.Combine(baseString, baseName + i.ToString("D6") + "_" + DateTime.Now);
}
我在原始文件中添加了DateTime.Now
,方法如下:
private string GetDirectoryName(string baseString, string baseName, int startAt)
{
int i = startAt;
while (Directory.Exists(Path.Combine(baseString, baseName + i.ToString("D6"))))
{
i++;
}
return Path.Combine(baseString, baseName + i.ToString("D6"));
}
然后根据需要创建目录:
string eventDir = GetDirectoryName(AnimatedGifDirectory, "Rain event ", 1);
if (!Directory.Exists(eventDir))
{
Directory.CreateDirectory(eventDir);
}
在使用DateTime.Now
之前,它工作正常,但是在添加DateTime.Now
之后,我遇到了异常。
这是一个目录示例,其中给出了eventDir
包含以下示例的异常:
C:\test\Rain event 000001_10/26/2014 8:09:12 AM
创建的方法的目录名称是这部分:
Rain event 000001_10/26/2014 8:09:12 AM
也许是2014年第8至8点和AM至12点之后的空格引起了问题? 也许目录名称应类似于:
Rain event 000001_10/26/2014_8:09:12_AM ?
我想做的是,仅创建诸如Rain事件000001,Rain事件000002和Rain事件000003之类的目录,因为这是一个事件,它将为每个事件和ID赋予名称。
因此,也许可以以任何方式添加日期和时间。 这样的事情。 因此目录仍将是Rain事件000001,但其后带有一些日期和时间ID。
它是:
字符,在Windows OS中是非法路径字符。 参见MSDN 。
您也可以调用Path.GetInvalidPathChars
方法来确定路径字符串中的字符是否无效。
使用DateTime
导致目录名称使用非法字符,或者,您也可以使用DateTime.Now.ToFileTime()
来表示当前时间(如果您同时执行或循环执行,则不一定是唯一的)。
根据Directory.CreateDirectory
的MSDN文档,如果目录名称包含不属于驱动器标签的冒号,则抛出NotSupportedException。 当您包含DateTime
您会在目录名称中包含一个冒号。
\ / : * ? " < > |
以上所有内容在文件名中均无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.