-5

How come this string is valid to open with VLC via a Process:

string fileToPlay = @"C:\Videos\Movies\Movie title.avi";

But this one isn't:

string fileToPlay = @myMovie;

Where the value of the variable myMovie is

"C:\Videos\Movies\Movie title.avi"

Process.Start(vlcPath, fileToPlay );
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • 3
    Why are you prefixing variable? – Giorgi Nakeuri Nov 28 '15 at 20:39
  • 2
    http://stackoverflow.com/questions/556133/whats-the-in-front-of-a-string-in-c notes what the use of `@` does that may be what you are missing here. – JB King Nov 28 '15 at 20:40
  • How did you give `myMovie` its value? – Lasse V. Karlsen Nov 28 '15 at 20:40
  • 1
    so what is this.. ? `string fileToPlay = @myMovie;` you need to read some `C# Basics tutorials` focusing on variables and what the `Literal sign @` means also `"C:\Videos\Movies\Movie title.avi"` is not a valid path in C# you need to use `"\\"` – MethodMan Nov 28 '15 at 20:41
  • 2
    ^ +1. I think you want this `var myMovie = @"C:\Videos\Movies\Movie title.avi"; string fileToPlay = myMovie;` – Arghya C Nov 28 '15 at 20:42

1 Answers1

1

The problem is that you can only use the @ character when placed against string literals like this:

string path = @"c:\temp";

It can be used when placed against a string variable, as you have done, but it has a different meaning. In that case, it is used when you choose an identifier which matches a C# keyword, like this:

string @class = "hello";

You can read more about it here: https://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx

  • It's a different meaning but, actually, you can put the at-sign in front of a variable name. You need to do this when an identifier is a [C# keyword](https://msdn.microsoft.com/en-us/library/x53a06bb.aspx). The purpose is so that C# is not limiting the names allowed by the .NET framework and it allows code generators to be oblivious to C# keywords. – Tom Blodget Nov 28 '15 at 21:11
  • Yes, @TomBlodget, you're right. Thank you for adding that. I'll see if I can correct my answer. – Stephen Moon Nov 28 '15 at 21:26