C# 判断文件和文件夹是否存在并创建
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# 判断文件和文件夹是否存在并创建的更多相关文章
- vc++ 判断文件或是文件夹是否存在,比较好的做法
#include <windows.h> void main() { //文件或文件夹都可以判断,最后的\\号有无都没关系 !=GetFileAttributes("D:\\My ...
- C#判断文件及文件夹是否存在并创建(C#判断文件夹存在)
protected void Button1_Click(object sender, EventArgs e) { if (Directory.Exists(Server.MapPath(" ...
- shell bash判断文件或文件夹是否存在
#shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...
- 【Python备忘】python判断文件和文件夹是否存在
python判断文件和文件夹是否存在 import os os.path.isfile('test.txt') #如果不存在就返回False os.path.exists(directory) #如果 ...
- matlab判断文件或文件夹是否存在
当前目录中包含以下文件及文件夹: startup.m win64/ … 判断当前目录中是否存在startup.m文件 if ~exist('startup.m','file')==0 error ...
- Shell 判断文件或文件夹是否存在
#shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...
- JAVA之旅(二十八)——File概述,创建,删除,判断文件存在,创建文件夹,判断是否为文件/文件夹,获取信息,文件列表,文件过滤
JAVA之旅(二十八)--File概述,创建,删除,判断文件存在,创建文件夹,判断是否为文件/文件夹,获取信息,文件列表,文件过滤 我们可以继续了,今天说下File 一.File概述 文件的操作是非常 ...
- c 判断文件或文件夹是否存在,多种方法, 为什么从一开始就不直接来个统一的呢?
具体内容,请看: https://blog.csdn.net/u012494876/article/details/51204615 判断文件或文件夹是否存在,竟然有这么多方法: GetFileAtt ...
- Java如何判断文件或者文件夹是否在?不存在如何创建?
Java如何判断文件或者文件夹是否在?不存在如何创建? 1. 首先明确一点的是:test.txt文件可以和test文件夹同时存在同一目录下:test文件不能和test文件夹同时存在同一目录下. 原 ...
- java判断给定路径或URL下的文件或文件夹是否存在?
if (file.exists()) { 来判断这是不是一个文件. file.isDirectory() 来判断这是不是一个文件夹. 1.File testFile = new File(testFi ...
随机推荐
- Deepin-安装(读写文件)权限
在安装NODE管理模块N时,遇到了权限问题 1.给予程序读写权限(仅限文件夹) 查看权限:ls -l 或 ls 添加权限: 示例:chmod +rw xx 实例:chmod +rw node 关于权限 ...
- c++学习笔记之基础---类内声明线程函数的调用
近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数exec_task中调用pthread_create去启动线程执行例程thread_rounter.编译之后报错如下: spf ...
- HDU 4277 USACO ORZ(暴力+双向枚举)
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- JDBC访问数据库查询信息的步骤(硬编码格式)
1 Class.forName()加载驱动 2 DriverManager获取Connect连接 3 创建Statement执行SQL语句 4 返回ResultSet查询结果 5释放资源 packag ...
- Linux文件锁【转】
本文转载自:http://blog.csdn.net/dragon_li_chen/article/details/17147911 一.文件锁的分类: 翻阅参考资料,你会发现文件锁可以进行很多的分类 ...
- ACTION中获得数据的几种方式
1.第一种是通过公司封装的方法. 2.第二种:是通过IF方法判断 3.第三种是通过:set/get获得
- uCos-III移植到STM32F10x
最近在百度上看了uCOS-III 的介绍后,诸多功能有很大的提升和改进,感觉有必要升级一下开发环境.百度介绍:http://baike.baidu.com/view/8531313.htm 环境: S ...
- 【转】使用git将项目上传到github(最简单方法)
原文地址:http://www.cnblogs.com/cxk1995/p/5800196.html 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ ...
- Overview of MIDI
东拼西凑的介绍 MIDI which means Musical Instrument Digital Interface, introduced in 1980's provided a inter ...
- 【180】IDL 读写 HDF 文件
HDF(Hierarchical Data Formats)数据格式由 NCSA 开发.HDF 提供了大量的数据模式,包括多维数组.表格.图像.注解和调色板.在下面的章节中,将描述 HDF 科学数据系 ...