如果文件夹不存在,则创建它 [英] If a folder does not exist, create it

查看:52
本文介绍了如果文件夹不存在,则创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了 FileUploader 控件.我想将文件保存到指定的文件夹.如果此文件夹不存在,我想先创建它,然后将我的文件保存到此文件夹中.如果文件夹已经存在,则只需将文件保存在其中.

I use a FileUploader control in my application. I want to save a file to a specified folder. If this folder does not exist, I want to first create it, and then save my file to this folder. If the folder already exists, then just save the file in it.

我该怎么做?

推荐答案

正如其他人所说,使用 System.IO.Directory.CreateDirectory.

As others have said, use System.IO.Directory.CreateDirectory.

但是,您不需要先检查它是否存在.来自文档:

But, you don't need to check if it exists first. From the documentation:

创建路径中指定的任何和所有目录,除非它们已经存在或路径的某些部分无效.如果目录已经存在,则此方法不会创建新目录,但会返回现有目录的 DirectoryInfo 对象.

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.

这篇关于如果文件夹不存在,则创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆