How to create date file in iso string in name?

Asked

Viewed 1,064 times

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 appears this message:

No support for the given path format.

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

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

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

How do I create like this?

  • You could inform the result that appears from this path + dateHora + "_test.txt concatenation"

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

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

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

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.