2013-03-13 41 views
4

我必須得使用HttpRuntime.AppDomainAppPath(這樣C:/personal/Website/page.aspx如何檢查父文件夾

Web服務始終位於父文件夾(如本C:/personal/Service/service.asmx)的page.aspx父我的網站路徑。我使用servicePath這個字符串servicePath="C:/personal/Service/service.asmx"這個變量,使用ABC.dll中的web.dll服務路徑。

如何根據網站路徑檢查服務路徑?

If (GetWebPath()== GetServicePath()) 
{ 
    // ... do something 
}  

private string GetWebPath() 
    { 
     string path = HttpRuntime.AppDomainAppPath; 
     string[] array = path.Split('\\'); 
     string removeString = ""; 
     for(int i = array.Length; --i >= 0;) 
     { 
      removeString = array[array.Length - 2]; 
      break; 
     } 
     path = path.Replace(@"\" + removeString + @"\", ""); 
     return path; 
    } 

    private string GetServicePath() 
    { 
     string path = @"C:\MNJ\OLK\ABC.asmx" 
     string[] array = path.Split('\\'); 
     string removeString = ""; 
     for(int i = array.Length; --i >= 0;) 
     { 
      removeString = @"\" + array[array.Length - 2] + @"\" + array[array.Length - 1]; 
      path = path.Replace(removeString, ""); 
      break; 
     } 
     return path; 
    } 
+0

伊諾什麼ü不明白,請告訴我,我會闡述它... – John 2013-03-13 09:32:31

+0

伊諾看到下面的評論.... – John 2013-03-13 10:23:24

+0

服務路徑d: \ data \ webspace \ MPS.3.0 \ SPM \ Server.asmx – John 2013-03-13 10:24:02

回答

1
string webPath = @"C:\blabla\CS_Web\website\"; 
string servicePath = @"C:\blabla\CS_Web\SPM\Server.asmx"; 

if(Path.GetDirectory(Path.GetDirectoryName(servicePath))==Path.GetDirectoryName(webPath) 
{ 
    //You do something here 
} 

你得最多頁面使用Path.GetDirectoryName父文件夾()

1

試試這個:

System.Web.Server.MapPath(webPath); 

這將返回當前正在執行的網頁文件的物理文件路徑。

更多信息可以在這裏找到:System.Web.Server

+0

我已經獲得了兩條路徑,我只想匹配那兩條路徑... – John 2013-03-13 09:34:45

1

提供要檢查以下pathes:

string webPath = @"C:\blabla\CS_Web\website\"; 
string servicePath = @"C:\blabla\CS_Web\SPM\Server.asmx"; 

你應該叫

string webPathParentDir = GetParentDirectoryName(webPath); 
string servicePathParentDir = GetParentDirectoryName(servicePath); 

if (servicePathParentDir.Equals(webPathParentDir, StringComparison.OrdinalIgnoreCase)) 
{ 
    // ... do something 
} 

與方法:

private string GetParentDirectoryName(string path) 
{ 
    string pathDirectory = Path.GetDirectoryName(servicePath); 

    return new DirectoryInfo(pathDirectory).Parent.FullName; 
} 
+0

異常詳細信息:System.ArgumentException:路徑中的非法字符 – John 2013-03-13 10:11:04

+0

您能指定含有這些非法字符的路徑嗎 – Olexander 2013-03-13 10:13:56

+0

服務路徑D:\ data \ webspace \ MPS.3.0 \ SPM \ Server.asmx – John 2013-03-13 10:15:53