国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#獲得程序的根目錄以及判斷文件是否存在的實例講解

2020-01-24 00:04:39
字體:
來源:轉載
供稿:網友

一:獲取根目錄的方法

取得控制臺應用程序的根目錄方法

方法1、Environment.CurrentDirectory 取得或設置當前工作目錄的完整限定路徑

方法2、AppDomain.CurrentDomain.BaseDirectory 獲取基目錄,它由程序集沖突解決程序用來探測程序集

取得WinForm應用程序的根目錄方法

1、Environment.CurrentDirectory.ToString();//獲取或設置當前工作目錄的完全限定路徑

2、Application.StartupPath.ToString();//獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱

3、Directory.GetCurrentDirectory();//獲取應用程序的當前工作目錄

4、AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來探測程序集

5、AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設置包含該應用程序的目錄的名稱

取得web應用程序的根目錄方法

1.HttpContext.Current.Server.MapPath("~/configs/ChannelUsers.xml")

HttpContext.Current

返回當前請求的 HttpContext 對象。如此我們就可以直接訪問Request、Response、Session、Application等對象,和Page中訪問等同。

我們無需再將Page用參數的方式傳遞到我們的類庫對象中。

HttpContext.Current.Session["name"] = "豬八戒";string name = HttpContext.Current.Request.Param["name"];HttpContext.Current.Response.Write("豬八戒好吃懶做!");

獲取網站根目錄的方法有幾種如:

Server.MapPath(Request.ServerVariables["PATH_INFO"])Server.MapPath("/")Server.MapPath("")Server.MapPath(".")Server.MapPath("../")Server.MapPath("..") Page.Request.ApplicationPath

運行結果:

C:/Inetpub/wwwroot/EnglishClub/manage/WebForm1.aspx
C:/Inetpub/wwwroot/
C:/Inetpub/wwwroot/EnglishClub/manage
C:/Inetpub/wwwroot/EnglishClub/manage
C:/Inetpub/wwwroot/EnglishClub/
C:/Inetpub/wwwroot/EnglishClub

以上的方法可以在.aspx中訪問,但是如果你在。cs文件就不能用。

HttpContext.Current.Server.MapPath();System.Web.HttpContext.Current.Request.PhysicalApplicationPath 

在.cs文件中可以用。

但是HttpContext.Current.Server.MapPath();這個獲取的是文件的路徑而不是根目錄。

只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath 這個才是獲取的根目錄,在寫獲取數據庫路徑是應該用這個,其他的都有問題。

二:判斷文件及文件夾是否存在的方法

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") +"http://"+ name;//獲取文件的實際路徑   string fpath = Server.MapPath("upfile") + "http://" + 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#獲得程序的根目錄以及判斷文件是否存在的實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江孜县| 北流市| 钦州市| 抚宁县| 六安市| 黄骅市| 蒙阴县| 贵阳市| 万全县| 胶州市| 随州市| 原平市| 松溪县| 靖安县| 淄博市| 哈尔滨市| 临海市| 广东省| 泰宁县| 兴安盟| 平罗县| 城口县| 天长市| 四子王旗| 娱乐| 肥乡县| 庄河市| 鹤峰县| 井研县| 白朗县| 肥西县| 卢龙县| 同仁县| 星座| 应城市| 宁安市| 盐边县| 鞍山市| 汾阳市| 鞍山市| 永年县|