-2

I want to split a process filepath by "\", but C# don't accepting '\'.

string[] pathSplit = p.MainModule.FileName.Split(new Char[] { '\' });
foreach(string path in pathSplit)                        
{
}

I got this error: https://i.stack.imgur.com/Dsfxb.png

The string doesn't end. Where is my problem?

  • 1
    Backslash in C# (`\`) is an escape character. to use it as a literal do as @GrantWinney suggested, or (if it is a string, which it's not in this case) use the `@` to make a literal string - i.e., `string myString = @"This is a string with \ a backslash";` – Tim Nov 05 '14 at 22:28
  • 2
    Perhaps you need to read the section on [string literals](http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx) on the C# reference – Steve Nov 05 '14 at 22:28

1 Answers1

3

You need to escape the '\' char in string or char literals.

You can just use @'\' instead to tell the compiler you need a '\'

Stewart_R
  • 13,764
  • 11
  • 60
  • 106