Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date as folder name

Fast one, why this code isnt working for me:

Directory.CreateDirectory(DateTime.ToString("dd-MM-yyyy"));

Erorr:

Error   1   An object reference is required for the non-static field, method, or property 'System.DateTime.ToString(string)'    Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs    83  39  WindowsFormsApplication1

What is wrong? And if I would like to have folders name as "This is folder of" and then add todays date, how should it look?

like image 852
Tautvydas Avatar asked Jan 02 '13 23:01

Tautvydas


2 Answers

Maybe you mean:

Directory.CreateDirectory(DateTime.Now.ToString("dd-MM-yyyy"));
like image 180
Mir Avatar answered Oct 04 '22 08:10

Mir


What is wrong?

ToString is an instance method not a static one, therefore you can't call it on DateTime class directly - you need to call it on an instance of the DateTime class.

And if I would like to have folders name as "This is folder of" and then add todays date, how should it look?

You can use the Now/UtcNow property of the DateTime class which would give you the current time instance e.g.

DateTime.UtcNow.ToString("dd-MM-yyyy");
like image 29
James Avatar answered Oct 04 '22 07:10

James