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;
}
}
}
}

C# 判断文件和文件夹是否存在并创建的更多相关文章

  1. vc++ 判断文件或是文件夹是否存在,比较好的做法

    #include <windows.h> void main() { //文件或文件夹都可以判断,最后的\\号有无都没关系 !=GetFileAttributes("D:\\My ...

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

    protected void Button1_Click(object sender, EventArgs e) { if (Directory.Exists(Server.MapPath(" ...

  3. shell bash判断文件或文件夹是否存在

    #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...

  4. 【Python备忘】python判断文件和文件夹是否存在

    python判断文件和文件夹是否存在 import os os.path.isfile('test.txt') #如果不存在就返回False os.path.exists(directory) #如果 ...

  5. matlab判断文件或文件夹是否存在

    当前目录中包含以下文件及文件夹: startup.m win64/ … 判断当前目录中是否存在startup.m文件 if ~exist('startup.m','file')==0    error ...

  6. Shell 判断文件或文件夹是否存在

    #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...

  7. JAVA之旅(二十八)——File概述,创建,删除,判断文件存在,创建文件夹,判断是否为文件/文件夹,获取信息,文件列表,文件过滤

    JAVA之旅(二十八)--File概述,创建,删除,判断文件存在,创建文件夹,判断是否为文件/文件夹,获取信息,文件列表,文件过滤 我们可以继续了,今天说下File 一.File概述 文件的操作是非常 ...

  8. c 判断文件或文件夹是否存在,多种方法, 为什么从一开始就不直接来个统一的呢?

    具体内容,请看: https://blog.csdn.net/u012494876/article/details/51204615 判断文件或文件夹是否存在,竟然有这么多方法: GetFileAtt ...

  9. Java如何判断文件或者文件夹是否在?不存在如何创建?

    Java如何判断文件或者文件夹是否在?不存在如何创建?   1. 首先明确一点的是:test.txt文件可以和test文件夹同时存在同一目录下:test文件不能和test文件夹同时存在同一目录下. 原 ...

  10. java判断给定路径或URL下的文件或文件夹是否存在?

    if (file.exists()) { 来判断这是不是一个文件. file.isDirectory() 来判断这是不是一个文件夹. 1.File testFile = new File(testFi ...

随机推荐

  1. Visual Studio VS如何卸载Visual assistant

    1 点击工具-扩展管理器   2 选中Visual Assist X,点击卸载即可.                            

  2. onfocus事件,onblur事件;Focus()方法,Blur()方法

    <1> <pre name="code" class="html"><!DOCTYPE html PUBLIC "-// ...

  3. 用Visual Studio高版本号打开低版本号的project,转换时出现错误:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    解决方法是: 在电脑里面搜索发现   C:\Program Files\Microsoft Visual Studio 10.0\VC\bin   C:\Windows\winsxs\x86_netf ...

  4. &lt;一&gt;读&lt;&lt;大话设计模式&gt;&gt;之简单工厂模式

    工厂模式尽管简单.可是写下这篇文章却不简单. 第一:本人经过内心的挣扎后才决定開始写博文的.为什么呢,由于好长时间没有写了,对自己的文学功底也是好不自信.可是技术这东西你不写出来你真不知道自己掌握多少 ...

  5. 自己定义Gradle插件之&quot;Hello World&quot;

    自己定义Gradle插件之"Hello World" 0.新建一个用于开发这个插件的目录 1.确定Plugin id Plugin id一般定义为java 包名. 由字母和数字及& ...

  6. 【Mongodb教程 第九课 】MongoDB 删除文档

    remove() 方法 MongoDB的 remove() 方法用于从集合中删除文档.remove() 方法接受两个参数.第一个是删除criteria ,第二是justOne标志: deletion ...

  7. Android反复闹钟(每天)的实现

    MainActivity例如以下: package cc.cc; import java.util.Calendar; import java.util.Locale; import android. ...

  8. 【Jquery】jQuery获取URL參数的两种方法

    jQuery获取URL參数的关键是获取到URL,然后对URL进行过滤处理,取出參数. location.href是取得URL.location.search是取得URL"?"之后的 ...

  9. 比 git log 更强大的 git reflog

    最近做了个骚操作 git checkout commitId 修改了部分内容 git add . git commit -m '修改了些东西'   -> 此时git 会自动生成一个新的 comm ...

  10. 第3周课后实践&#183;程序阅读(4)-利用引用訪问私有数据成员

    /* * Copyright (c) 2015, 烟台大学计算机学院 * All rights reserved. * 文件名:test.cpp * 作 者:刘畅 * 完毕日期:2015年 3 月 2 ...