Search code examples
c#textftpextractlastindexof

Extracting text from ftp address


I'm creating small FTP Client and stuck on small issue, can you help me to sort it out please.

So I'm taking text from comboBox1.Text witch is lets say "/test/sql/it/" But for creating new directory I need to extract "it" and "/test/sql/" "it" as new directory name and "/test/sql/" location for creating new folder.

For second part I can use:

string s = comboBox1.Text;
        s = s.Remove(s.LastIndexOf('/'));
        s = s.Remove(s.LastIndexOf('/'));
        s = s + "/";
        MessageBox.Show(s);
        //result "/test/sql/"

But how to get first part "it" any one?


Solution

  • try this,

    string s = comboBox1.Text;
    string path_s = Path.GetFileName( Path.GetDirectoryName( path ) );
    

    The inner call to GetDirectoryName will return the full path, while the outer call to GetFileName() will return the last path component - which will be the folder name.