本文实例报告了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; } } }
以上就是C#完成推断文件夹存在与否并建立文件夹的示例代码的内容,更多相干内容请关注ki4网(www.ki4.cn)!