locked
how to create a function for change directory RRS feed

  • Question

  • User569149469 posted
    using System;
    
    public class Path
    {
        public string CurrentPath { get; private set; }
    
        public Path(string path)
        {
            this.CurrentPath = path;
        }
    
        public void Cd(string newPath)
        {
            string strPath=newPath.Split("/");
            
           
        }
    
        public static void Main(string[] args)
        {
            Path path = new Path("/a/b/c/d");
            path.Cd("../x");
            Console.WriteLine(path.CurrentPath);
        }
    }
    For example:
    
    Path path = new Path("/a/b/c/d");
    path.Cd("../x");
    Console.WriteLine(path.CurrentPath);
    should display '/a/b/c/x'.

    Saturday, August 18, 2018 6:17 AM

All replies

  • User753101303 posted

    Hi,

    If working on virtual path you could use System.Uri:

    var uri= new System.Uri("http://root/a/b/c/d/../x");
    Console.WriteLine(uri.AbsolutePath); // Shows /a/b/c/x

    Edit: for an ASP.NET app the concept of a "current" folder or virtual path is a bit weird. You may want to discuss directly your final goal.

    Sunday, August 19, 2018 7:34 PM