Tautvydas
Tautvydas

Reputation: 1288

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?

Upvotes: 5

Views: 11351

Answers (3)

James
James

Reputation: 82136

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");

Upvotes: 5

Dmytro
Dmytro

Reputation: 17196

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

Upvotes: 4

e_ne
e_ne

Reputation: 8469

Maybe you mean:

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

Upvotes: 18

Related Questions