preethi
preethi

Reputation: 905

creating a new folder with todays date on specific folder

I am trying to create a new folder with todays date on specific given path:

string LocalDirectory = Directory.CreateDirectory(
    DateTime.Now.ToString("I:\\test\\final test\\snaps\\dd-MM-yyyy")); 

But I receive this error:

Cannot implicitly convert type 'System.IO.DirectoryInfo' to 'string'

Upvotes: 1

Views: 17356

Answers (7)

Joe
Joe

Reputation: 69

I wanted to create directories for the year and then the month inside the year folder. Here's what worked for me:

            public void CreateDirectory()
            {
            string strArchiveFolder = (@"\\fullpath" + DateTime.Now.Year.ToString() + "\\" +
                                  DateTime.Now.Month.ToString());
            if (!Directory.Exists(strArchiveFolder))
            {
            Directory.CreateDirectory(strArchiveFolder);
            }

Upvotes: 0

Krishan Dutt Sharma
Krishan Dutt Sharma

Reputation: 216

string path = Server.MapPath(@"/Content/");             
path = Path.Combine(path,DateTime.Now.ToString('ddmmyyyy'));
if (!Directory.Exists(path))
  {
     Directory.CreateDirectory(path);
  }

Upvotes: 0

Andrei Krasutski
Andrei Krasutski

Reputation: 5267

Take into account the culture

var rootOutputDir = @"I:\test\final test\snaps";
var Todaysdate = DateTime.Now.ToString(CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern.Replace("/", "-"));
Directory.CreateDirectory(Path.Combine(rootOutputDir, Todaysdate));

Upvotes: 0

Aubrey Love
Aubrey Love

Reputation: 1034

Here is about the simplest way of creating a new folder named with todays date.

using System;


namespace CreateNewFolder
{
    class Program
    {
        static void Main(string[] args)
        {
            string Todaysdate = DateTime.Now.ToString("-dd-MM-yyyy-(hh-mm-ss)");

            {
                Directory.CreateDirectory("c:/Top-Level Folder/Subfolder/Test" + Todaysdate);
            }
        }
    }
}

Output of New folder name:

Test-02-05-2018-(11-05-02)

I put the hours, minutes and seconds inside some parentheses for clarity. You can take out any part of the date to return only the time/date portion you want to call your folder. If you don’t want to call it “Test-02-05-2018-(11-05-02)” but simply have todays date as the name; like “02-05-2018”, then remove the “Test” from the “CreateDirectory” line but leave a blank space between -Subfolder/ and the closing quotation mark. Like this:

Directory.CreateDirectory("c:/Top-Level Folder/Subfolder/ " + Todaysdate);

Notice that I added a hyphen between the date parameters. This is just a visual separator for the date, you could also use a “space” as the separator.

I know this string is about 4 years old, but maybe this will help another newbie just starting out in C#.
Enjoy and share.

Upvotes: 0

Iti Tyagi
Iti Tyagi

Reputation: 3661

The code can be written as :

String Todaysdate = DateTime.Now.ToString("dd-MMM-yyyy");
if(!Directory.Exists("I:\\test\\final test\\snaps\\" + Todaysdate)
{
  Directory.CreateDirectory("I:\\test\\final test\\snaps\\" + Todaysdate); 
}

Upvotes: 1

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13765

Directory.CreateDirectory return a DirectoryInfo not string

you can try something like this

DirectoryInfo LocalDirectory = Directory.CreateDirectory(string.Format("I:\\test\\final test\\snaps\\{0}-{1}-{2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year));

to get the path as string

string strLocalDir = LocalDirectory.FullName; 

Upvotes: 0

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391446

As per the documentation for Directory.CreateDirectory, CreateDirectory returns a DirectoryInfo object, not a string.

So do this:

DirectoryInfo localDirectory = Directory.CreateDirectory(...

or this:

var localDirectory = Directory.CreateDirectory(...

(which will basically do the same thing)

Upvotes: 3

Related Questions