您的位置:首页 > 编程语言 > C#

C#判断文件和文件夹是否存在 不存在则创建

2013-09-27 11:30 711 查看
if (Directory.Exists(Server.MapPath(~/upimg/hufu)) == false)//如果不存在就创建file文件夹{
Directory.CreateDirectory(Server.MapPath(~/upimg/hufu));}
//Directory.Delete(Server.MapPath(~/upimg/hufu), true);//删除文件夹以及文件夹中的子目录,文件
//判断文件的存在
if (File.Exists(Server.MapPath(~/upimg/Data.html))){
Response.Write(Yes);//存在文件}else{
Response.Write(No);
//不存在文件
File.Create(MapPath(~/upimg/Data.html));//创建该文件}
string name = GetFiles.FileName;//获取已上传文件的名字
string size = GetFiles.PostedFile.ContentLength.ToString();//获取已上传文件的大小
string type = GetFiles.PostedFile.ContentType;//获取已上传文件的MIME
string postfix = name.Substring(name.LastIndexOf(.) + 1);//获取已上传文件的后缀
string ipath = Server.MapPath(upimg) +\\+ name;//获取文件的实际路径
string fpath = Server.MapPath(upfile) + \\ + name;
string dpath = upimg\\ + name;//判断写入数据库的虚拟路径
ShowPic.Visible = true;//激活
ShowText.Visible = true;//激活
//判断文件格式
if (name == ) {
Response.Write(<scriptalert('上传文件不能为空')</script);}else{
if (postfix == jpg || postfix == gif || postfix == bmp || postfix == png){
GetFiles.SaveAs(ipath);
ShowPic.ImageUrl = dpath;
ShowText.Text = 你上传的图片名称是: + name +  + 文件大小: + size + KB +  + 文件类型: + type +  + 存放的实际路径为: + ipath;}else{
ShowPic.Visible = false;//隐藏图片
GetFiles.SaveAs(fpath);//由于不是图片文件,因此转存在upfile这个文件夹
ShowText.Text = 你上传的文件名称是: + name +  + 文件大小: + size + KB +  + 文件类型: + type +  + 存放的实际路径为: + fpath;}

-----------------------------------------------------------------------------------------------------------------

public partial class Default3 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){ShowPic.Visible = false;//初始化不显示
ShowText.Visible = false;//初始化不显示}protected void Button1_Click(object sender, EventArgs e){
if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)//如果不存在就创建file文件夹{Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));}
//Directory.Delete(Server.MapPath("~/upimg/hufu"), true);//删除文件夹以及文件夹中的子目录,文件
//判断文件的

--------------------------------------------------------------------------------------------------------------------

C# 如何判断指定路径的文件夹是否存在

2010-04-28 20:23一枪爆三个 | 分类:C#/.NET | 浏览4064次
我的代码:  DirectoryInfo mydir = new DirectoryInfo(dirPath1);
FileInfo[] files = mydir.GetFiles();
如果指定的路径dirpath1的文件夹不存在 那么就无法执行第二句代码 所以我要在第一句后面加一个判断,判断是否存在 请问如何判断?

引用System.IO命名空间
if(Directory.Exists(dirPath1))
{
FileInfo[] files = mydir.GetFiles();
}
else
{
Directory.CreateDirectory(dirPath1);
FileInfo[] files = mydir.GetFiles();
}

-------------------------------------------------------------------

C#判断文件及文件夹是否存在并创建(C#判断文件夹存在)


protected void Button1_Click(object sender, EventArgs e)
{if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));
}//Directory.Delete(Server.MapPath("~/upimg/hufu"), true);//删除文件夹以及文件夹中的子目录,文件//判断文件的存在if (File.Exists(Server.MapPath("~/upimg/Data.html")))
{
Response.Write("Yes");//存在文件}else
{
Response.Write("No");
//不存在文件
File.Create(MapPath("~/upimg/Data.html"));//创建该文件}string name = GetFiles.FileName;//获取已上传文件的名字
string size = GetFiles.PostedFile.ContentLength.ToString();//获取已上传文件的大小
string type = GetFiles.PostedFile.ContentType;//获取已上传文件的MIME
string postfix = name.Substring(name.LastIndexOf(".") + 1);//获取已上传文件的后缀
string ipath = Server.MapPath("upimg") +"\\"+ name;//获取文件的实际路径
string fpath = Server.MapPath("upfile") + "\\" + name;
string dpath = "upimg\\" + name;//判断写入数据库的虚拟路径ShowPic.Visible = true;//激活
ShowText.Visible = true;//激活//判断文件格式
if (name == "") {
Response.Write("<script>alert('上传文件不能为空')</script>");
}else{if (postfix == "jpg" || postfix == "gif" || postfix == "bmp" || postfix == "png")
{
GetFiles.SaveAs(ipath);
ShowPic.ImageUrl = dpath;
ShowText.Text = "你上传的图片名称是:" + name + "<br>" + "文件大小:" + size + "KB" + "<br>" + "文件类型:" + type + "<br>" + "存放的实际路径为:" + ipath;}else
{
ShowPic.Visible = false;//隐藏图片
GetFiles.SaveAs(fpath);//由于不是图片文件,因此转存在upfile这个文件夹
ShowText.Text = "你上传的文件名称是:" + name + "<br>" + "文件大小:" + size + "KB" + "<br>" + "文件类型:" + type + "<br>" + "存放的实际路径为:" + fpath;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: