Search code examples
c#directory

Fail to make directory in C#


Below is my code to create a directory in my PC.

using System;
using System.IO;
using System.Text;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {

            DirectoryInfo dataDir = new DirectoryInfo(@"C:\CsharpData");
            Console.WriteLine(dataDir.Attributes);

            Console.ReadLine();
        }
    }
}

But, the result looks like this.

Attribute is equal to -1, and I can't get my desired directory.

Can anyone let me know what my mistake is?

enter image description here


Solution

  • Use below code. You need to use create ,ethod for creating the directory.

    DirectoryInfo dataDir = new DirectoryInfo(@"C:\CsharpData");
    
    if(!dataDir.Exists)
    {
        dataDir.Create();
    }
    System.Console.WriteLine(dataDir.Attributes);
    
    System.Console.ReadLine();