zoukankan      html  css  js  c++  java
  • 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;

    }
  • 相关阅读:
    XML 加密、解密
    word 转 pdf
    PowerDesigner(PowerDesigner15.1.0.2850)下载、安装以及破解
    SQL 2005 18456
    SQL 2005端口的修改
    SQL——统计查询
    由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例 解决办法
    ASP.NET MVC中Model View Controller的开发顺序
    使用Windows身份验证的Intranet网站安全管理 Windows Authentication
    ASP.NET web.config中数据库连接字符串connectionStrings节的配置
  • 原文地址:https://www.cnblogs.com/szytwo/p/2411703.html
Copyright © 2011-2022 走看看