c# - 如果文件夹不存在,则创建它

标签 c# asp.net directory

我在我的应用程序中使用了一个 FileUploader 控件。我想将文件保存到指定的文件夹。如果这个文件夹不存在,我想先创建一个,然后把我的文件保存到这个文件夹中。如果该文件夹已经存在,则只需将文件保存在其中即可。

我该怎么做?

最佳答案

使用System.IO.Directory.CreateDirectory

据官方".NET" docs ,你不需要先检查它是否存在。

System.io   >   Directory   >   Directory.CreateDirectory

Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.

        — learn.microsoft.com/dotnet/api/

关于c# - 如果文件夹不存在,则创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9065598/

相关文章:

c# - 使用 Linq to SQL SELECT @@DBTS

c# - 用代码修复 'The DELETE statement conflicted with the REFERENCE constraint'错误

asp.net - 操作数数据类型 varchar 对于求和运算符无效 - 过程错误更正

windows - 当脚本具有像/usr/games/lib 这样的默认目录时,Perl for Windows 会在哪里查找?

xcode - 将文件移动到 Xcode 中的真实文件夹中

c# - .Net 标准库中是否有 .Each() (或 .ForEach() )迭代器?

c# - 如何将一个 "clone"对象转化为子类对象?

windows - 在 Windows 命令行中将子文件夹上传到 FTP

c# - 如何使用正则表达式和 C# 忽略字符串中的额外空格?

javascript - 为什么我的 JavaScript 不与我的 UserControl 交互?