Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建带有日期的文件时.net不支持异常_.net_File_Datetime_C++ Cli_Notsupportedexception - Fatal编程技术网

创建带有日期的文件时.net不支持异常

创建带有日期的文件时.net不支持异常,.net,file,datetime,c++-cli,notsupportedexception,.net,File,Datetime,C++ Cli,Notsupportedexception,我想创建一个带有日期的文件 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

我想创建一个带有日期的文件

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

我认为问题在于:
我该如何修复它呢?

我个人会用“\u”替换“.”和“:”


strPath.Replace(“.”,“”).Replace(“:”,“”)

我个人会将“.”和“:”替换为“u”


strPath.Replace(“.”,“”).Replace(“:”,“”)

用下划线替换每个无效字符:

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代码。

用下划线替换每个无效字符:

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代码。

问题仅限于
是文件名的有效字符。后者不需要被替换。@DevendraD.Chavan true,我还建议丢失“.”,因为文件名有后缀,惯例是“.”分隔后缀。这是一个非常具体问题的快速解决方案,不可重用,不安全,无法防止格式更改。您可以检查每个无效字符,并用我提供的以下解决方案替换它。@Baboon,同意,您的是一个更好的解决方案,这就是为什么我对它投了更高的票。@Mylescdonnell如果只是这一个,在他的整个解决方案中从未发生过变化,并且被称为数千次,那么您的解决方案是一个更好(性能更好)的解决方案。这就是为什么我要投票;)问题仅限于
是文件名的有效字符。后者不需要被替换。@DevendraD.Chavan true,我还建议丢失“.”,因为文件名有后缀,惯例是“.”分隔后缀。这是一个非常具体问题的快速解决方案,不可重用,不安全,无法防止格式更改。您可以检查每个无效字符,并用我提供的以下解决方案替换它。@Baboon,同意,您的是一个更好的解决方案,这就是为什么我对它投了更高的票。@Mylescdonnell如果只是这一个,在他的整个解决方案中从未发生过变化,并且被称为数千次,那么您的解决方案是一个更好(性能更好)的解决方案。这就是为什么我要投票;)