Tav*_*usi 667 c# asp.net directory
我在我的应用程序中使用FileUploader控件.我想将文件保存在指定的文件夹中.现在我想,如果此文件夹不存在,首先创建它,然后将我的文件保存到此文件夹.如果该文件夹已存在,则只需将文件保存在其中.
我怎么能这样做?
Mar*_*ers 1171
正如其他人所说,使用 System.IO.Directory.CreateDirectory
但是,您不需要先检查它是否存在.来自文档
创建路径中指定的任何和所有目录,除非它们已存在或除非路径的某些部分无效.如果该目录已存在,则此方法不会创建新目录,但会返回现有目录的DirectoryInfo对象.
Rav*_*via 343
根据http://forums.asp.net/p/1226236/2209871.aspx使用以下代码:
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));
Run Code Online (Sandbox Code Playgroud)
Nic*_*oul 211
只需写下这一行:
System.IO.Directory.CreateDirectory("my folder");
Run Code Online (Sandbox Code Playgroud)
参考:有关MSDN上的Directory.CreateDirectory的文章
jer*_*enh 28
Directory.CreateDirectory 这将解释如何检查FilePath是否存在
Directory.Exists 这将解释如何尝试创建FilePath(如果它不存在)
Den*_*aub 27
如果路径尚不存在,则可以使用如下方法创建路径:
using System.IO;
private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(Server.MapPath(path));
if (!folderExists)
Directory.CreateDirectory(Server.MapPath(path));
}
Run Code Online (Sandbox Code Playgroud)
Met*_*Man 15
您可以使用try/catch子句并检查它是否存在:
try
{
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
Run Code Online (Sandbox Code Playgroud)
Bla*_*ear 15
using System.IO
if (!Directory.Exists(yourDirectory))
Directory.CreateDirectory(yourDirectory);
Run Code Online (Sandbox Code Playgroud)
小智 15
如果不存在,此方法将创建文件夹,如果存在则不执行任何操作
Directory.CreateDirectory(path);
Run Code Online (Sandbox Code Playgroud)
Kir*_*kar 13
if (!Directory.Exists(Path.GetDirectoryName(fileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}
Run Code Online (Sandbox Code Playgroud)
以下代码是我使用的最佳代码行,如果不存在则将创建目录.
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/temp/"));
Run Code Online (Sandbox Code Playgroud)
如果该目录已存在,则此方法不会创建新目录,但会返回现有目录的DirectoryInfo对象.>
string pathToNewFolder = System.IO.Path.Combine(parentFolderPath, "NewSubFolder");
DirectoryInfo directory = Directory.CreateDirectory(pathToNewFolder);
// Will create if does not already exist (otherwise will ignore)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
737220 次 |
| 最近记录: |