Replace words dynamically

3

For example, I have the file addresses:

  

C: /windows/x.exe

     

C: /windwos/system32/example.exe

I would like to replace the filename and extension with "", thus:

  

C: / Windows /

     

C: / windows / system32 /

    
asked by anonymous 10.04.2015 / 13:34

1 answer

6

You can use the Path.GetDirectoryName method

var path1 = @"C:/windows/x.exe";
var path2 = @"C:/windwos/system32/exemplo.exe";
Console.WriteLine(Path.GetDirectoryName(path1) + Path.DirectorySeparatorChar);
Console.WriteLine(Path.GetDirectoryName(path2) + Path.DirectorySeparatorChar);

DotNetFiddle

    
10.04.2015 / 13:50