如何创建目录,如果它不存在,创建文件?

如何创建目录,如果它不存在,创建文件?

问题描述:

我有一张code在这里打破,如果目录不存在:

I have a piece of code here that breaks if the directory doesn't exist:

System.IO.File.WriteAllText(filePath, content);

在一行(或几行),是有可能检查是否导致新文件的目录不存在,如果没有,在创建新文件之前创建?

In one line (or a few lines), is it possible to check if the directory leading to the new file doesn't exist and if not, to create it before creating the new file?

我使用.NET 3.5。

I'm using .NET 3.5.

(新的FileInfo(文件路径))。Directory.Create()之前写入文件。

System.IO.FileInfo file = new System.IO.FileInfo(filePath);
file.Directory.Create(); // If the directory already exists, this method does nothing.
System.IO.File.WriteAllText(file.FullName, content);