


C# implements sample code to determine whether a folder exists and create a folder
这篇文章主要介绍了C#实现判断文件夹存在与否并创建文件夹的方法,涉及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; } } }
以上就是C#实现判断文件夹存在与否并创建文件夹的示例代码的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.
