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



    c# 应用知识汇集 2008-09-14 14:19:57 阅读564 评论0 字号:

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

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.IO;

    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);//删除文件夹以及文件夹中的子目录,文件    

             //判断文件的存在

             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;

                 }

            

             }

         }

  • 相关阅读:
    [转贴]Asp.Net[C#]在线压缩解压
    [转载]如何用C#语言构造蜘蛛程序
    [转载].net 集合类初步认识
    试用随笔
    安装中文VS2008 SP1 和.NETFRAMEWORK 3.5SP1后智能提示是英文的解决办法
    关于IE提示“Internet Explorer无法打开站点,已终止操作”的解决办法
    linux每天建立一个以当天日期命名的文件夹
    linux常用装机命令
    linux安装oracle客户端
    批量分发文件
  • 原文地址:https://www.cnblogs.com/nianshi/p/1750083.html
Copyright © 2020-2023  润新知