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

时间:2022-12-20 02:54:38

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?

在一行(或几行)中,是否可以检查指向新文件的目录是否不存在,如果不存在,是否可以在创建新文件之前创建它?

I'm using .NET 3.5.

我用。net 3.5。

5 个解决方案

#1


300  

(new FileInfo(filePath)).Directory.Create() Before writing to the file.

(新的FileInfo(文件路径)). directory.create()在写入文件之前。

Or

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);

#2


90  

You can use following code

您可以使用以下代码

  DirectoryInfo di = Directory.CreateDirectory(path);

#3


27  

As @hitec said, you have to be sure that you have the right permissions, if you do, you can use this line to ensure the existence of the directory:

正如@hitec所说,你必须确保你有正确的权限,如果你有,你可以使用这一行来确保目录的存在:

Directory.CreateDirectory(Path.GetDirectoryName(filePath))

Directory.CreateDirectory(Path.GetDirectoryName(filePath))

#4


-1  

You can use File.Exists to check if the file exists and create it using File.Create if required. Make sure you check if you have access to create files at that location.

您可以使用文件。存在以检查文件是否存在并使用文件创建它。如果需要创建。确保检查是否有访问该位置的文件。

Once you are certain that the file exists, you can write to it safely. Though as a precaution, you should put your code into a try...catch block and catch for the exceptions that function is likely to raise if things don't go exactly as planned.

一旦确定该文件存在,就可以安全地写入它。尽管作为预防措施,您应该尝试一下您的代码……如果事情没有按照计划进行,那么捕获块并捕获函数可能会增加的异常。

Additional information for basic file I/O concepts.

基本文件I/O概念的附加信息。

#5


-1  

var filePath = context.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["ErrorLogFile"]));

var filePath = context.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings[" ErrorLogFile "]));

var file = new FileInfo(filePath);

var文件=新的FileInfo(filePath);

file.Directory.Create(); If the directory already exists, this method does nothing.

file.Directory.Create();如果该目录已经存在,该方法什么也不做。

var sw = new StreamWriter(filePath, true);

var sw =新StreamWriter(filePath, true);

sw.WriteLine(Enter your message here);

西南。WriteLine(在这里输入您的消息);

sw.Close();

sw.Close();

#1


300  

(new FileInfo(filePath)).Directory.Create() Before writing to the file.

(新的FileInfo(文件路径)). directory.create()在写入文件之前。

Or

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);

#2


90  

You can use following code

您可以使用以下代码

  DirectoryInfo di = Directory.CreateDirectory(path);

#3


27  

As @hitec said, you have to be sure that you have the right permissions, if you do, you can use this line to ensure the existence of the directory:

正如@hitec所说,你必须确保你有正确的权限,如果你有,你可以使用这一行来确保目录的存在:

Directory.CreateDirectory(Path.GetDirectoryName(filePath))

Directory.CreateDirectory(Path.GetDirectoryName(filePath))

#4


-1  

You can use File.Exists to check if the file exists and create it using File.Create if required. Make sure you check if you have access to create files at that location.

您可以使用文件。存在以检查文件是否存在并使用文件创建它。如果需要创建。确保检查是否有访问该位置的文件。

Once you are certain that the file exists, you can write to it safely. Though as a precaution, you should put your code into a try...catch block and catch for the exceptions that function is likely to raise if things don't go exactly as planned.

一旦确定该文件存在,就可以安全地写入它。尽管作为预防措施,您应该尝试一下您的代码……如果事情没有按照计划进行,那么捕获块并捕获函数可能会增加的异常。

Additional information for basic file I/O concepts.

基本文件I/O概念的附加信息。

#5


-1  

var filePath = context.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings["ErrorLogFile"]));

var filePath = context.Server.MapPath(Convert.ToString(ConfigurationManager.AppSettings[" ErrorLogFile "]));

var file = new FileInfo(filePath);

var文件=新的FileInfo(filePath);

file.Directory.Create(); If the directory already exists, this method does nothing.

file.Directory.Create();如果该目录已经存在,该方法什么也不做。

var sw = new StreamWriter(filePath, true);

var sw =新StreamWriter(filePath, true);

sw.WriteLine(Enter your message here);

西南。WriteLine(在这里输入您的消息);

sw.Close();

sw.Close();