.net NotSupportedException при создании файла с датой - PullRequest
0 голосов
/ 05 января 2012

Я хочу создать файл с датой.

DateTime time_now = DateTime::UtcNow;
String^  time_str = time_now.UtcNow.ToString();
String^  strPath = "C:\\Users\\Documents\\VS\\MyProject\\" + fileName + time_str + ".prc";

FileStream^  fs = File::Create(strPath); // in this line I get notSupportedException

Я отлаживаю код и имя файла: myfile05.01.2012 12: 37: 1222.prc

Я думаю, что проблемы ":" Как я могу это исправить?

Ответы [ 2 ]

3 голосов
/ 05 января 2012

Лично я бы заменил "."и с "_" ;

strPath.Replace(".","_").Replace(":","_");

2 голосов
/ 05 января 2012

Заменить каждый недопустимый символ подчеркиванием:

private string GetValidPath(string _Path)
        {
            String goodPath = _Path;
            foreach (char letter in System.IO.Path.GetInvalidPathChars())
            {
                goodPath = goodPath.Replace(letter, '_');
            }
            return goodPath;
        }

Если вы программируете на C ++ / CLI, вы можете перенести этот код C #.

...