-1

How to create directory work internally in c#.

I am creating directory like

"C:\ProgramData\Temp\Temp1\Temp2\Temp3".
This "Temp\Temp1\Temp2\Temp3"

is not present. currently.

After I call method create directory in C#.

how to create directory? is it create one by one internally? like "Temp" -> "Temp1" and so on?

If it is "Temp/Temp1/Temp2" is created and another program delete Temp1 directory before creating Temp2 so the exception is thrown? like directory not found?

Amit Verma
  • 2,450
  • 2
  • 8
  • 21
  • Look at this answer: https://stackoverflow.com/questions/1680836/create-directory-sub-directories/1680841 – JohnnyPrognose Jul 22 '21 at 12:37
  • Is it create one by one? internally? – Archit Soni Jul 22 '21 at 12:42
  • In .NET Framework 4.8, it creates the directories one at a time, as you can see in the [source code](https://referencesource.microsoft.com/#mscorlib/system/io/directory.cs,214). – Andrew Morton Jul 22 '21 at 12:46
  • 1
    Does this answer your question? [How to create multiple directories from a single full path in C#?](https://stackoverflow.com/questions/2134392/how-to-create-multiple-directories-from-a-single-full-path-in-c) – dennis_ler Jul 22 '21 at 13:29

2 Answers2

1

Directory.CreateDirectory does that already:

Creates all the directories in a specified path.

For your question

If it is "Temp/Temp1/Temp2" is created and another program delete Temp1 directory before creating Temp2 so the exception is thrown?

I think it's quite unlikely. Why should another program delete a directory that doesn't even exist yet?

The method may throw many exception, one of them probably in this unlucky case. You can try to implement that: create a thread that permanently deletes the directory in an endless loop, then create another thread which creates the directory.

is it create one by one internally?

Even if it might be possible to answer this question, you should neither care nor rely on that implementation. Microsoft could change it with any update of the .NET framework. It could also depend on the file system. Perhaps they use TxF (file system transactions) if the file system supports it, like NTFS, but they can't for FAT32.

On Windows, they simply call the CreateDirectory Windows API (line 329 in the source code) but they could also switch to CreateDirectoryTransacted Window API

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Is it create one by one? internally? C# code? – Archit Soni Jul 22 '21 at 12:42
  • @ArchitSoni: why do you care? MS could change it with any update of the .NET framework. It could also depend on the file system. Perhaps they use TxF if the file system supports it (like NTFS), but they can't for FAT32 – Thomas Weller Jul 22 '21 at 12:42
  • Okay thanks. But internally check before it is already created or not? like "Temp1" or "Temp2"? – Archit Soni Jul 22 '21 at 12:44
0

As said Thomas Weller , Directory.CreateDirectory in C# makes the job.
For you question :

If it is "Temp/Temp1/Temp2" is created and another program delete Temp1 directory before creating Temp2 so the exception is thrown?

I dont't understand the point and why would another app delete your directory.
If it is the case, this will an exception in the other app and the app which created the directory will have an exception when trying to acces this directory.