C#獲得程序的根目錄以及判斷文件是否存在的實(shí)例講解
一:獲取根目錄的方法
取得控制臺(tái)應(yīng)用程序的根目錄方法
方法1、Environment.CurrentDirectory 取得或設(shè)置當(dāng)前工作目錄的完整限定路徑
方法2、AppDomain.CurrentDomain.BaseDirectory 獲取基目錄,它由程序集沖突解決程序用來(lái)探測(cè)程序集
取得WinForm應(yīng)用程序的根目錄方法
1、Environment.CurrentDirectory.ToString();//獲取或設(shè)置當(dāng)前工作目錄的完全限定路徑
2、Application.StartupPath.ToString();//獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑,不包括可執(zhí)行文件的名稱
3、Directory.GetCurrentDirectory();//獲取應(yīng)用程序的當(dāng)前工作目錄
4、AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來(lái)探測(cè)程序集
5、AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設(shè)置包含該應(yīng)用程序的目錄的名稱
取得web應(yīng)用程序的根目錄方法
1.HttpContext.Current.Server.MapPath("~/configs/ChannelUsers.xml")
HttpContext.Current
返回當(dāng)前請(qǐng)求的 HttpContext 對(duì)象。如此我們就可以直接訪問Request、Response、Session、Application等對(duì)象,和Page中訪問等同。
我們無(wú)需再將Page用參數(shù)的方式傳遞到我們的類庫(kù)對(duì)象中。
HttpContext.Current.Session["name"] = "豬八戒"; string name = HttpContext.Current.Request.Param["name"]; HttpContext.Current.Response.Write("豬八戒好吃懶做!");
獲取網(wǎng)站根目錄的方法有幾種如:
Server.MapPath(Request.ServerVariables["PATH_INFO"]) Server.MapPath("/") Server.MapPath("") Server.MapPath(".") Server.MapPath("../") Server.MapPath("..") Page.Request.ApplicationPath
運(yùn)行結(jié)果:
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();這個(gè)獲取的是文件的路徑而不是根目錄。
只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath 這個(gè)才是獲取的根目錄,在寫獲取數(shù)據(jù)庫(kù)路徑是應(yīng)該用這個(gè),其他的都有問題。
二:判斷文件及文件夾是否存在的方法
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)//如果不存在就創(chuàng)建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"));//創(chuàng)建該文件 } 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;//獲取文件的實(shí)際路徑 string fpath = Server.MapPath("upfile") + "\\" + name; string dpath = "upimg\\" + name;//判斷寫入數(shù)據(jù)庫(kù)的虛擬路徑 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>" + "存放的實(shí)際路徑為:" + ipath; } else { ShowPic.Visible = false;//隱藏圖片 GetFiles.SaveAs(fpath);//由于不是圖片文件,因此轉(zhuǎn)存在upfile這個(gè)文件夾 ShowText.Text = "你上傳的文件名稱是:" + name + "<br>" + "文件大小:" + size + "KB" + "<br>" + "文件類型:" + type + "<br>" + "存放的實(shí)際路徑為:" + fpath; } } } }
以上這篇C#獲得程序的根目錄以及判斷文件是否存在的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Silverlight文件上傳下載實(shí)現(xiàn)方法(下載保存)
這篇文章主要介紹了Silverlight文件上傳下載實(shí)現(xiàn)方法(下載保存) ,需要的朋友可以參考下2015-11-11C#中創(chuàng)建PDF網(wǎng)格并插入圖片的方法
這篇文章我將向大家演示如何以編程的方式在PDF文檔中創(chuàng)建一個(gè)網(wǎng)格,并將圖片插入特定的網(wǎng)格中。對(duì)c# pdf 網(wǎng)格 插入圖片的知識(shí)感興趣的朋友一起看看吧2016-11-11C#在復(fù)雜多線程環(huán)境下使用讀寫鎖同步寫入文件
這篇文章介紹了C#在復(fù)雜多線程環(huán)境下使用讀寫鎖同步寫入文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04C#運(yùn)算符大全_各種運(yùn)算符號(hào)的概述及作用
以下是對(duì)C#中各種運(yùn)算符號(hào)的說(shuō)明及作用進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10