There is a string

C:\Folder1\SubFolder1\SubFolder2\SubFolder3 

I have to cut everything for the last slash and get

 C:\Folder1\SubFolder1\SubFolder2 

How to implement it?

    1 answer 1

    Vapiant 1:

     var path = @"C:\Folder1\SubFolder1\SubFolder2\SubFolder3"; var result = System.IO.Path.GetDirectoryName(path); 

    For reference: GetDirectoryName

    Option 2:

     var path = @"C:\Folder1\SubFolder1\SubFolder2\SubFolder3"; int pos = path.LastIndexOf("\\"); // позиция последнего слеша var result = path.Remove(pos, path.Length - pos); 

    Result:

    C: \ Folder1 \ SubFolder1 \ SubFolder2

    • and if it is perceived as a text? It is on another computer. You need to get a new line on it and send it. That is, it must be text, without System.IO.Path - Rakzin Roman
    • Thank you. The second option is what you need - Rakzin Roman
    • @RakzinRoman But why do not System.IO.Path please you and what do you mean by "perceived as text"? - Pavel Mayorov
    • Ruslan_K, why not Substring? - Pavel Mayorov