2013-03-13 40 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