How to check parent folder

I have my website path using HttpRuntime.AppDomainAppPath (e.g. this is C:/personal/Website/page.aspx )

The web service is always located on the parent page.aspx passport of the parent folder (for example, C:/personal/Service/service.asmx ). I get the webservice path using ABC.dll in the servicePath variable, like this line servicePath="C:/personal/Service/service.asmx" .

How to check the path to the service on the way to the website?

 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; } 
+4
source share
3 answers
 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 } 

You need to open the page in the parent folder using Path.GetDirectoryName ()

+1
source

Try the following:

 System.Web.Server.MapPath(webPath); 

This will return the path to the physical file of the current executable web file.

Further information can be found here: System.Web.Server

+1
source

If you want to check the following patterns:

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

you should call

 string webPathParentDir = GetParentDirectoryName(webPath); string servicePathParentDir = GetParentDirectoryName(servicePath); if (servicePathParentDir.Equals(webPathParentDir, StringComparison.OrdinalIgnoreCase)) { // ... do something } 

using the method:

 private string GetParentDirectoryName(string path) { string pathDirectory = Path.GetDirectoryName(servicePath); return new DirectoryInfo(pathDirectory).Parent.FullName; } 
+1
source

Source: https://habr.com/ru/post/1468907/


All Articles