How to compare directory path names?

0

I have two paths:

  • C:\anacarvalho\Database\Updates\2017\2017_04\20170419_AC - Path selected by folder browser dialog .
  • C:\anacarvalho\Database\Updates - Path that is the root of the system.
  • I need a way, that tells me if the path that was selected by folder browser dialog , contains the same beginning of path 2. In this case you have to tell me that until the Updates is equal.

    Can someone help me?

        
    asked by anonymous 09.06.2017 / 11:09

    1 answer

    3

    You can do it this way:

    string caminho = "C:\anacarvalho\Database\Updates\2017\2017_04170419_AC";
    string raiz = "C:\anacarvalho\Database\Updates";
    if (caminho.Contains(raiz))
    {
    //Código aqui.
    }
    

    Or this:

    string caminho = "C:\anacarvalho\Database\Updates\201717_04\20170419_AC";
    string raiz = "C:\anacarvalho\Database\Updates";
    if(caminho.IndexOf(raiz,StringComparison.OrdinalIgnoreCase) != -1)
    {
    //Código aqui.
    }
    

    See working at Ideone .

        
    09.06.2017 / 12:00