如果文件夹不存在,如何创建文件和文件夹?
想象一下,我希望创建(或覆盖)以下文件:- C:\Temp\Bar\Foo\Test.txt
使用 File.Create(..) 方法,这个可以做到。
但是,如果我没有以下任一文件夹(来自上面的示例路径)
- Temp
- Bar
- Foo
,那么我会得到一个 DirectoryNotFoundException 抛出。
所以..给定一个路径,我们如何递归地创建为该路径创建文件..所需的所有文件夹?如果 Temp 或 Bar 文件夹存在,但 Foo 不存在...那么也被创建。
为了简单起见,我们假设没有安全问题——所有权限都很好,等等。
Imagine I wish to create (or overwrite) the following file :- C:\Temp\Bar\Foo\Test.txt
Using the File.Create(..) method, this can do it.
BUT, if I don't have either one of the following folders (from that example path, above)
- Temp
- Bar
- Foo
then I get an DirectoryNotFoundException thrown.
So .. given a path, how can we recursively create all the folders necessary to create the file .. for that path? If Temp or Bar folders exists, but Foo doesn't... then that is created also.
For simplicity, lets assume there's no Security concerns -- all permissions are fine, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
总结其他答案中评论的内容:
Directory.CreateDirectory
将递归地创建目录,如果目录已经存在,它将返回而不会出现错误。如果
C:\Temp\Bar\Foo
处恰好有一个文件Foo
,则会抛出异常。To summarize what has been commented in other answers:
Directory.CreateDirectory
will create the directories recursively and if the directory already exist it will return without an error.If there happened to be a file
Foo
atC:\Temp\Bar\Foo
an exception will be thrown.请参阅此 MSDN 页面。
See this MSDN page.
使用 Directory.CreateDirectory在创建文件之前。它会为您递归地创建文件夹。
Use Directory.CreateDirectory before you create the file. It creates the folder recursively for you.
创建路径指定的所有目录和子目录。
然后你可以创建一个文件。
Creates all directories and subdirectories as specified by path.
then you may create a file.
您需要检查路径的两个部分(目录和文件名),如果不存在则创建每个部分。
使用
File.Exists
和Directory.Exists
来查明它们是否存在。 Directory.CreateDirectory 将为您创建整个路径,因此如果目录不存在,您只需调用一次,然后只需创建文件即可。You will need to check both parts of the path (directory and filename) and create each if it does not exist.
Use
File.Exists
andDirectory.Exists
to find out whether they exist.Directory.CreateDirectory
will create the whole path for you, so you only ever need to call that once if the directory does not exist, then simply create the file.您应该使用 Directory.CreateDirectory。
http://msdn.microsoft.com/en-us/library/54a0at6s。 ASPX
You should use Directory.CreateDirectory.
http://msdn.microsoft.com/en-us/library/54a0at6s.aspx
假设您的程序集/exe 具有 FileIO许可本身就是不对的。您的应用程序可能无法以管理员权限运行。重要的是要考虑代码访问安全和< a href="http://support.microsoft.com/kb/842419" rel="nofollow noreferrer">请求 权限
示例代码:
了解 .NET 代码访问安全性
“代码访问安全性”在现实世界中有用吗?
Assuming that your assembly/exe has FileIO permission is itself, well is not right. Your application may not run with admin rights. Its important to consider Code Access Security and requesting permissions
Sample code:
Understanding .NET Code Access Security
Is “Code Access Security” of any real world use?
你想要 Directory.CreateDirectory()
这里是我使用的一个类(转换为 C#),如果你向它传递一个源目录和一个目标,它将将该目录的所有文件和子文件夹复制到你的目标:
}
You want Directory.CreateDirectory()
Here is a class I use (converted to C#) that if you pass it a source directory and a destination it will copy all of the files and sub-folders of that directory to your destination:
}
以下代码将创建目录(如果不存在)&然后复制文件。
Following code will create directories (if not exists) & then copy files.