9

I want to create a file that is in specific directory. For example: C:\x\y\z\aTextFile.txt

For this operation, I have to create the directory to create file.

Directory.CreateDirectory(@"C:\x\y\z\");
File.Create(@"C:\x\y\z\aTextFile.txt");

But I really wonder that I can do this operation in single line of code.

Any help and idea will be greately appreciated.

ds4940
  • 3,382
  • 1
  • 13
  • 20
ibrahimyilmaz
  • 18,331
  • 13
  • 61
  • 80

6 Answers6

4

Simple: Add a function

void MySingleLineOfCodeFunction(string path, string filename)
{
    Directory.Createdirectory(path);
    File.Create(filename).Dispose();
}

and then use a single line of code:

MySingleLineOfCodeFunction(@"C:\x\y\z\", "a.txt");

What I am trying to say is that there is no difference between code. Some of it is written by the Microsoft guys, while other by us, normal people. But the computers don't make a difference. :)

malat
  • 12,152
  • 13
  • 89
  • 158
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
2

Unfortunately no there is no single line of code to do what you want.

Why? Because even if we do so by some Microsoft inbuilt function, internally it will be calling two methods. One for Directory creation and other for file creation.

However you can reduce your lines of code by making them into a method and call it in a single line as Petar Ivanov said

OR

You can create a static extension method. This way you can use it at other places without creating instance. (Reduced your one line where instance is created).

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
2

As far I know, there is no File creation method that create the directory at the same time in the .NET framework.

If the pattern "Directory check/creation, then file creation" is repeated a lot in your code, you have to implement it in a method.

Larry
  • 17,605
  • 9
  • 77
  • 106
2

I've modified the answer from @Petar_Ivanov, to work out the parent directory based on the file path, thought it may be useful to others who might want a similar function.

    public void CreateFile(string filePath)
    {
        if (!File.Exists(filePath))
        {
            var parent = Directory.GetParent(filePath);
            Directory.CreateDirectory(parent.FullName);
            File.Create(filePath).Dispose();
        }
    }
malat
  • 12,152
  • 13
  • 89
  • 158
EdmundYeung99
  • 2,461
  • 4
  • 27
  • 43
1

For creating a file, no, the directory has to exist. Even with Visual Basic's file friendly classes, you still have to create the directories first. What is interesting is that moving will create the folder.

From CreateFile http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx

Directories

An application cannot create a directory by using CreateFile, therefore only the OPEN_EXISTING value is valid for dwCreationDisposition for this use case. To create a directory, the application must call CreateDirectory or CreateDirectoryEx

AMissico
  • 21,470
  • 7
  • 78
  • 106
0

Mostly in my code blocks; I handle folder existence right before file create parts.

public void CheckCreatePath(fileName)
{
    string filePath = Directory.GetParent(fileName).ToString();
    if (!Directory.Exists(filePath))
        Directory.CreateDirectory(filePath);
}

and just use

CheckCreatePath("C:\\TEMP\\TESTPath\\myFile.txt");

in your code block.