为什么我无法创建此目录名称?获取:不支持给定路径的格式

时间:2017-01-19 21:48:48

标签: c# .net winforms

List<string> countryPaths = new List<string>();
        private void CreateCountryDateTimeDirectories(List<string> urls)
        {
            for (int i = 0; i < countriesNames.Count(); i++)
            {
                string pathDateTime = urls[0].Substring(48, 12);
                string pathDateTimeLast = urls[urls.Count - 1].Substring(48, 12);
                var d = DateTime.ParseExact(pathDateTime, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
                var e = DateTime.ParseExact(pathDateTimeLast, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
                string country = countriesNames[i].Substring(15);
                string countryPath = countriesMainPath + "\\" + country + "\\" + "[" + d + "---" + e + "]";
                if (!Directory.Exists(countryPath))
                {
                    Directory.CreateDirectory(countryPath);
                }
                countryPaths.Add(countryPath);
            }
        }

在线

Directory.CreateDirectory(countryPath);

我得到:不支持给定路径的格式

例如,它尝试创建并提供例外的第一个目录:

C:\ Users \ me \ AppData \ Local \ s \ s \ Countries \ Europe \ [19/01/2017 19:15:00 --- 19/01/2017 21:30:00]

我猜问题是最后一部分:[19/01/2017 19:15:00 --- 19/01/2017 21:30:00] 那么我该如何创建像这样的目录呢?与日期和时间范围?

2 个答案:

答案 0 :(得分:5)

在路径中的任何位置(驱动器号除外)都不能有:

/将成为目录分隔符,这不是你想要的。

答案 1 :(得分:1)

如果你想避免麻烦,用_替换所有特定字符也许它不会那么好但是会更安全

不允许使用以下字符(至少在Windows上)

/\:*?"<>|