How to create file with date in iso string in name?

0

I'm having trouble creating a file with the following template:

2017-01-17T09:42:15.3419026-02:00_teste.txt

When I run the application to create, this message appears:

The given path format is not supported.

var dataHora = DateTimeOffset.Now.ToString("o");

dataHora.Replace(':', '-');
dataHora.Replace('/', '-');

File.WriteAllText(caminho + dataHora + "_teste.txt");

How do I create this?

    
asked by anonymous 17.01.2017 / 12:46

1 answer

1

Use the reference itself to change the result, in the form used the result of Replace () 'is not being used anywhere, nor saved, the change is lost. Contrary to what you may be imagining, the method does not change the object, it generates a new one, do so:

var dataHora = DateTimeOffset.Now.ToString("o").Replace(':', '-').Replace('/', '-');

See working on .NET Fiddle . And No Coding Ground . Also put it on GitHub for future reference .

    
17.01.2017 / 13:08