How to use MSDN and Visual Studio documentation to figure out what I need to "use" directives

When I need a tool and find it on MSDN, I often get delayed figuring out what directive using

I need to use it. For example, I need File.Exists(...)

, documented here , listed in the namespace System.IO

, but when I use it in my code, I get a compiler error

'System.Web.Mvc.Controller.File (string, string, string)' is a 'method' which is not valid in this context

It doesn't make sense because my context is

if (!File.Exists(newFileNameAndPath))
     throw new Exception(string.Format("File with name {0} already exists in Assets folder. Not overwritten.", newFileName));

      

is simple and I am using the System.IO;

above. So what's the problem?

I guess what I really need using System.IO.something

, but I'm not sure how I can figure out what it is something

.

+3


source to share


4 answers


You get this because it System.Web.Mvc.Controller

has a method File

and it also System.IO

has a class File

.

You can use the fully qualified class name to prevent conflict:



if (!System.IO.File.Exists(newFileNameAndPath))

      

+3


source


You will need to put your space in the file. Exists

eg:



if (!IO.File.Exists(newFileNameAndPath))

      

+3


source


You can explicitly specify which class File

you want to use using the alias directive :

using File = System.IO.File;

if (!File.Exists(newFileNameAndPath))

      

+2


source


The problem is that there are two named elements File

.

So, if you have both operators using

, you get a conflict. The solution is to either fully qualify one of them (ex:) System.IO.File.Exists(newFileNameAndPath)

or use an alias.

using IO = System.IO; //at the top of the file

...

if (!IO.File.Exists(newFileNameAndPath))

      

+1


source







All Articles