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
.
source to share
You can explicitly specify which class File
you want to use using the alias directive :
using File = System.IO.File;
if (!File.Exists(newFileNameAndPath))
source to share
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))
source to share