0

This is the simple C# code I wrote :

string file = @"D:\test(2021/02/10).docx";
            var fileName = Path.GetFileNameWithoutExtension(file);
            Console.WriteLine(fileName);

I thought I would get the string "test(2021/02/10)" , but I got this result "10)". How can I solve such a problem?

david.gao
  • 41
  • 8

1 Answers1

3

I just wonder why would you want such behavior. On windows slashes are treated as separator between directory and subdirectory (or file).

So, basically you are not able to create such file name.

And since slashes are treated as described, it is very natural that method implementation just checks what's after last slash and extracts just filename.

If you are interested on how the method is implemented take a look at source code

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • 2
    You *can* create that as a filename, it's just that it represents a directory under D: called `test(2021`, with a directory under that called `02`, with a file under that called `10).docx`. – Jon Skeet Aug 17 '21 at 08:14
  • @JonSkeet Hm you are correct (never even think of it that way to be honest :P ) - but I am trying to give "high level" of understanding :) without going into much details :) – Michał Turczyn Aug 17 '21 at 08:16