我在我的应用程序中使用FileUploader控件。我想保存一个文件到指定的文件夹。如果这个文件夹不存在,我想先创建它,然后将我的文件保存到这个文件夹。如果文件夹已经存在,那么只需将文件保存在其中。
我该怎么做呢?
我在我的应用程序中使用FileUploader控件。我想保存一个文件到指定的文件夹。如果这个文件夹不存在,我想先创建它,然后将我的文件保存到这个文件夹。如果文件夹已经存在,那么只需将文件保存在其中。
我该怎么做呢?
当前回答
using System.IO
if (!Directory.Exists(yourDirectory))
Directory.CreateDirectory(yourDirectory);
其他回答
目录中。CreateDirectory解释了如果FilePath不存在,如何尝试创建FilePath。
目录中。Exists命令用于检查FilePath是否存在。但是,您不需要这样做,因为CreateDirectory将为您检查它。
使用下面的代码按如何使用文件上传服务器控件动态创建文件夹?:
string subPath ="ImagesPath"; // Your code goes here
bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));
if(!exists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
就写这一行:
System.IO.Directory.CreateDirectory("my folder");
如果该文件夹还不存在,则将创建该文件夹。 如果文件夹已经存在,那么该行将被忽略。
参考:关于目录的文章。在MSDN创建目录
当然,你也可以使用System.IO;在源文件的顶部,然后只写目录。CreateDirectory(“我的文件夹”);每次你想创建一个文件夹。
using System.IO
if (!Directory.Exists(yourDirectory))
Directory.CreateDirectory(yourDirectory);
如果路径还不存在,可以使用如下方法创建路径:
using System.IO;
private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(Server.MapPath(path));
if (!folderExists)
Directory.CreateDirectory(Server.MapPath(path));
}