某些環境下用 System.Web.HttpContext.Current.Server.MapPath 取不到目錄。
可以用下面兩個方法:
一,System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' })
二,System.Web.HttpRuntime.AppDomainAppPath
///
<summary>
/// WebForm和WinForm通用的取當前根目錄的方法
/// </summary>
public static string BasePath
{
get
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
// WebDev.WebServer visual studio web server
// xxx.vhost Winform
// w3wp IIS7
// aspnet_wp IIS6
string processName = p.ProcessName.ToLower();
if (processName == " aspnet_wp " || processName == " w3wp " || processName == " webdev.webserver ")
{
if (System.Web.HttpContext.Current != null)
return System.Web.HttpContext.Current.Server.MapPath( " ~/ ").TrimEnd( new char[] { ' \\ ' });
else // 當控件在定時器的觸發程序中使用時就為空
{
return System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd( new char[] { ' \\ ' });
}
}
else
{
return System.Windows.Forms.Application.StartupPath;
}
}
}
/// WebForm和WinForm通用的取當前根目錄的方法
/// </summary>
public static string BasePath
{
get
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
// WebDev.WebServer visual studio web server
// xxx.vhost Winform
// w3wp IIS7
// aspnet_wp IIS6
string processName = p.ProcessName.ToLower();
if (processName == " aspnet_wp " || processName == " w3wp " || processName == " webdev.webserver ")
{
if (System.Web.HttpContext.Current != null)
return System.Web.HttpContext.Current.Server.MapPath( " ~/ ").TrimEnd( new char[] { ' \\ ' });
else // 當控件在定時器的觸發程序中使用時就為空
{
return System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd( new char[] { ' \\ ' });
}
}
else
{
return System.Windows.Forms.Application.StartupPath;
}
}
}
另外附些以前整理的,c#中獲取程序當前路徑的集中方法
string str1 =Process.GetCurrentProcess().MainModule.FileName; // 可獲得當前執行的exe的文件名。
string str2=Environment.CurrentDirectory; // 獲取和設置當前目錄(即該進程從中啟動的目錄)的完全限定路徑。
// 備注 按照定義,如果該進程在本地或網絡驅動器的根目錄中啟動,則此屬性的值為驅動器名稱后跟一個尾部反斜杠(如“C:\”)。如果該進程在子目錄中啟動,則此屬性的值為不帶尾部反斜杠的驅動器和子目錄路徑(如“C:\mySubDirectory”)。
string str3=Directory.GetCurrentDirectory(); // 獲取應用程序的當前工作目錄。
string str4=AppDomain.CurrentDomain.BaseDirectory; // 獲取基目錄,它由程序集沖突解決程序用來探測程序集。
string str5=Application.StartupPath; // 獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。
string str6=Application.ExecutablePath; // 獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase; // 獲取或設置包含該應用程序的目錄的名稱。
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
獲取模塊的完整路徑。
2. System.Environment.CurrentDirectory
獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄。
3. System.IO.Directory.GetCurrentDirectory()
獲取應用程序的當前工作目錄。這個不一定是程序從中啟動的目錄啊,有可能程序放在C:\www里,這個函數有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有時不一定返回什么東東,我也搞不懂了。
4. System.AppDomain.CurrentDomain.BaseDirectory
獲取程序的基目錄。
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
獲取和設置包括該應用程序的目錄的名稱。
6. System.Windows.Forms.Application.StartupPath
獲取啟動了應用程序的可執行文件的路徑。效果和2、5一樣。只是5返回的字符串后面多了一個 " \"而已
7. System.Windows.Forms.Application.ExecutablePath
獲取啟動了應用程序的可執行文件的路徑及文件名,效果和1一樣。