-1

I used a method that granted me a file path and I want to get only the file name. so I try to use string.split('\'). however, it seems like the computer waits for some another closing and does not let me contain this char as a separator. it expects either ',' or ';' depends on the content. quick fixing it with constant doesn't solve it as the problem moves recursively to the constant.

https://www.dropbox.com/s/95oon2i40mohb3k/split%20by%20slash.PNG?dl=0

any workaround?

edit: thanks for all the helpers, '\\' did do the trick :)

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
likuku
  • 358
  • 6
  • 21
  • 1
    A backslash is often used to "escape" the next character. To get an actual, single, backslash, use `'\\'`. – Andrew Morton Jul 22 '19 at 12:02
  • 1
    Possible duplicate of [Get file name from a path string in C#](https://stackoverflow.com/questions/6921105/get-file-name-from-a-path-string-in-c-sharp) – Liam Jul 23 '19 at 12:33

4 Answers4

3

You can use Path Class

https://learn.microsoft.com/en-us/dotnet/api/system.io.path?view=netframework-4.8

string path1 = @"c:\temp\MyTest.txt";
string strFileName = Path.GetFileName(path1)
Liam
  • 27,717
  • 28
  • 128
  • 190
Bhushan Muttha
  • 420
  • 2
  • 11
3

Try with two backslashes instead of one to double escape it.

path.Split('\\');
2

You need to escape the backslash character.

string.Split('\\');

You can find some more info here

Discobarry
  • 64
  • 3
2
path.Split(Path.DirectorySeparatorChar);
Liam
  • 27,717
  • 28
  • 128
  • 190
8twinni8
  • 21
  • 4