如何判断一个路径是否存在,不存在则创建

uniteworld 2010-08-01 12:52:59
要asp.net上传文件用,为每个用户组建立一个文件夹,用C#如何判断这个目录是否存在,如果不存在就创建
...全文
904 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
tackey86 2010-08-01
  • 打赏
  • 举报
回复
楼上所有的都回答的是正确的

System.IO.Directory.Exists("路径名");

就这个
Ny-6000 2010-08-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 meceky 的回复:]
引用 1 楼 asdf311 的回复:

Directory.Exists("路径名");

一般用这个方法了。
[/Quote]

就是么。
这个足够啦。
poloyzhang 2010-08-01
  • 打赏
  • 举报
回复


try
{
// string errorDirectory = @"D:\Program Files\error_salary"; //如果该目录不存在将创建
string errorPath = @"D:\Program Files\error_salary" + "\\" + fileName +".txt";
//start access file
if (!System.IO.File.Exists(errorPath))
{
// Create a file to write to.
using (System.IO.StreamWriter sw = System.IO.File.CreateText(errorPath))
{
sw.WriteLine( exceptionString ); //将导常写入文件.
}
}
else
{
// This text is always added, making the file longer over time
// if it is not deleted.
using (System.IO.StreamWriter sw2 = System.IO.File.AppendText(errorPath))
{
sw2.WriteLine();
// sw.WriteLine(sqlTable);
sw2.WriteLine(DateTime.Now + " ");
sw2.WriteLine( exceptionString );
}
}
//end

}
catch (Exception ec)
{
Console.WriteLine(ec.ToString());
}

poloyzhang 2010-08-01
  • 打赏
  • 举报
回复
try
{
// string errorDirectory = @"D:\Program Files\error_salary"; //如果该目录不存在将创建
string errorPath = @"D:\Program Files\error_salary" + "\\" + fileName +".txt";
//start access file
if (!System.IO.File.Exists(errorPath))
{
// Create a file to write to.
using (System.IO.StreamWriter sw = System.IO.File.CreateText(errorPath))
{
sw.WriteLine( exceptionString ); //将导常写入文件.
}
}
else
{
// This text is always added, making the file longer over time
// if it is not deleted.
using (System.IO.StreamWriter sw2 = System.IO.File.AppendText(errorPath))
{
sw2.WriteLine();
// sw.WriteLine(sqlTable);
sw2.WriteLine(DateTime.Now + " ");
sw2.WriteLine( exceptionString );
}
}
//end

}
catch (Exception ec)
{
Console.WriteLine(ec.ToString());
}
meceky 2010-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 asdf311 的回复:]

Directory.Exists("路径名");
[/Quote]
一般用这个方法了。
wuyq11 2010-08-01
  • 打赏
  • 举报
回复
protected void btnUpload_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile)
{
string savePath = Server.MapPath("~/upload/");
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
savePath = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(savePath);//保存文件
}
}

Peter200694013 2010-08-01
  • 打赏
  • 举报
回复
Exists 确定给定路径是否引用磁盘上的现有目录。

if (Directory.Exists(path))
{
Console.WriteLine("That path exists already.");
return;
}

// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));

// Delete the directory.
di.Delete();
Console.WriteLine("The directory was deleted successfully.");

lovenoerror 2010-08-01
  • 打赏
  • 举报
回复
补充一下
原文地址:http://technet.microsoft.com/zh-cn/sysinternals/system.io.directory.exists.aspx

经常上去看看微软挺好 经常都会给一些示例代码
lovenoerror 2010-08-01
  • 打赏
  • 举报
回复
Exists 方法
.NET Framework 类库
Directory..::.Exists
方法 确定给定路径是否引用磁盘上的现有目录。


命名空间: System.IO
程序集: mscorlib(在 mscorlib.dll 中)

示例

// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}


// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);

// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}

// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
}
wheeler 2010-08-01
  • 打赏
  • 举报
回复
Directory.Exists("路径名");

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧