C#判斷文件和文件夾是否存在 不存在則創建


using System.IO;
string path = @"D:\accountDaoRu\";
        if (Directory.Exists(path) == false)
        {
            Directory.CreateDirectory(path);
        }

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(<scriptalert('上傳文件不能為空')</script);}else{
if (postfix == jpg || postfix == gif || postfix == bmp || postfix == png){
GetFiles.SaveAs(ipath);
ShowPic.ImageUrl = dpath;
ShowText.Text = 你上傳的圖片名稱是: + name +  + 文件大小: + size + KB +  + 文件類型: + type +  + 存放的實際路徑為: + ipath;}else{
ShowPic.Visible = false;//隱藏圖片
GetFiles.SaveAs(fpath);//由於不是圖片文件,因此轉存在upfile這個文件夾
ShowText.Text = 你上傳的文件名稱是: + name +  + 文件大小: + size + KB +  + 文件類型: + type +  + 存放的實際路徑為: + fpath;}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM