.net NotSupportedException while creating file with date
-
14-04-2021 - |
Domanda
I want to create a file with date.
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
I debug the code and the file name is : myfile05.01.2012 12:37:1222.prc
I think the probles is ":" How can I fix it?
Soluzione
Personally I would replace the "." and ":" with "_" ;
strPath.Replace(".","_").Replace(":","_");
Altri suggerimenti
Replace every invalid character with an underscore:
private string GetValidPath(string _Path)
{
String goodPath = _Path;
foreach (char letter in System.IO.Path.GetInvalidPathChars())
{
goodPath = goodPath.Replace(letter, '_');
}
return goodPath;
}
If you're programming in C++/CLI, you can hopefully port this C# code.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow