c#與獲得文件夾路徑,站點目錄


本次情況是在系統內的3級目錄內,要上傳一文件,保存到系統的二級目錄,按以往知識:
FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + " \\news_pic\\" + FileUpload1.FileName);
可惜,不行啊,因為這里顯示的路徑,Server.MapPath(".")將會得到當前頁面所在的物理路徑.所以不能達到邏輯要求.
后來經過查詢,得到解決方法:
ileUpload1.PostedFile.SaveAs(Request .PhysicalApplicationPath +"/news_pic/" + FileUpload1.FileName);
下面轉一位高手的記錄,我懶,哈,學習了。
C#獲取項目程序及運行路徑的方法
1.asp.net webform用“Request.PhysicalApplicationPath獲取站點所在虛擬目錄的物理路徑,最后包含“\”;
 
2.c# winform用
A:“Application.StartupPath”:獲取當前應用程序所在目錄的路徑,最后不包含“\”;
B:“Application.ExecutablePath ”:獲取當前應用程序文件的路徑,包含文件的名稱;
C:“AppDomain.CurrentDomain.BaseDirectory”:獲取當前應用程序所在目錄的路徑,最后包含“\”;
D:“System.Threading.Thread.GetDomain().BaseDirectory”:獲取當前應用程序所在目錄的路徑,最后包含“\”;
E:“Environment.CurrentDirectory”:獲取當前應用程序的路徑,最后不包含“\”;
F:“System.IO.Directory.GetCurrentDirectory”:獲取當前應用程序的路徑,最后不包含“\”;
 
3.c# windows service服務中用“AppDomain.CurrentDomain.BaseDirectory”或“System.Threading.Thread.GetDomain().BaseDirectory”;
用“Environment.CurrentDirectory”和“System.IO.Directory.GetCurrentDirectory”將得到“ system32”目錄的路徑;
如果要使用“Application.StartupPath”或“Application.ExecutablePath ”,需要手動添加對“System.Windows.Forms.dll ”的引用,並在程序開頭用“using   System.Windows.Forms”聲明該引用;
 
4.在卸載程序獲取系統安裝的目錄:
         System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();
         string path=curPath.Location;//得到安裝程序類SetupLibrary文件的路徑,獲取這個文件路徑所在的目錄即得到安裝程序的目錄;
 
System.Diagnostics.StackFrame f = new System.Diagnostics.StackFrame(1);
                MethodBase mb = f.GetMethod();
                System.Web.HttpContext.Current.Response.Write(mb.DeclaringType.ToString()); 獲取調用類的信息,可以從父類知道子類的情況


免責聲明!

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



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