0

I am trying to create a new directory using Directory.CreateDirectory(), but I am unable to understand why it does not create a new directory when I use the following code.

var directory = Path.Combine(Environment.CurrentDirectory, "Status" + "-" + "Test" + Guid.NewGuid() + "\\");
Directory.CreateDirectory(directory);

But when I manually feed the file path it creates the directory (works well).

Directory.CreateDirectory(@"F:\Code\Help\");

Am I doing it wrong?

Any comments or feedback is greatly appreciated. TIA.

1
  • Sorry that was the wrong string I pasted "F:\\Code\\Help\\Status-Test-4cb61c98-bf52-42de-bb1e-c827c4a97e01"
    – Dazzler
    Commented Aug 3, 2018 at 4:50

1 Answer 1

3

There is nothing wrong with your code per-se

My suspicion is you are creating a directory (somewhere)

try
{
   var directory = Path.Combine(Environment.CurrentDirectory, $@"Status-Test{Guid.NewGuid()}");
   Console.WriteLine(directory);

   var di = Directory.CreateDirectory(directory);

   Console.WriteLine($"The directory was created successfully at {Directory.GetCreationTime(directory)}.");
   Console.WriteLine($"==> { di.FullName}");
}
catch (Exception e)
{
   Console.WriteLine("Oh NOES!: {0}", e);
}

Environment.CurrentDirectory Property

By definition, if this process starts in the root directory of a local or network drive, the value of this property is the drive name followed by a trailing slash (for example, "C:\"). If this process starts in a subdirectory, the value of this property is the drive and subdirectory path, without a trailing slash (for example, "C:\mySubDirectory").

Not the answer you're looking for? Browse other questions tagged or ask your own question.