7

I come from a VB.Net environment, where using Imports System and then IO.Directory.GetFiles(...) works.

On the other hand, it seems that using System; is not sufficient to write use IO.Directory without prefixing it with System.. The only workaround seems to be using IO = System.IO;

Why?


Example code:

using System;
using System.IO;

namespace Test {
    class Program {
        static void Main(string[] args) {
            System.Console.WriteLine(IO.Directory.GetFiles(System.Environment.CurrentDirectory)[0]);
        }
    }
}

Edit: My question is not what should I do to get my code working, but specifically "why cant I write IO.Directory.GetFiles ??"

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
Clément
  • 12,299
  • 15
  • 75
  • 115
  • Just bear in mind that you always get a small indication of your un-added namespaces at the bottom right the class name when you type the exact class name. You can expand it by hovering your mouse on the red indication and do the required. – nawfal Feb 24 '12 at 15:18

2 Answers2

8

Add

using System.IO;

and you'll have the behavior you expect. C# does not make child namespaces available without a using directive (or full qualification.)

Matt T
  • 511
  • 2
  • 8
  • +1 Nice, just of interest perhaps you have any references to MSDN regarding such requirement it would be great – sll Feb 24 '12 at 15:12
  • This yields `The name 'IO' does not exist in the current context` when I write `IO.Directory.GetFiles` – Clément Feb 24 '12 at 15:20
  • 1
    @Matt: ok but question is why `using System; is not sufficient to write use IO.Directory without prefixing it with System.`, so `using` exists but does not allow accessing concrete types of child namespeces without additional using for child namespace, like `IO.Directory.GetFiles()` without `usign System.IO` – sll Feb 24 '12 at 15:22
  • @Clément , add `using System.IO;` to your namespace collection at the top after which you can simply write `Directory.GetFiles(...)` – nawfal Feb 24 '12 at 15:23
  • If you added "using System.IO;" at the top of your class, you should be able to call System.IO.Directory.GetFiles(), IO.Directory.GetFiles(), and Directory.GetFiles(). If you can't do that, something else is wrong. If you'd like to post or send me some code I'd be happy to take a look. – Matt T Feb 24 '12 at 15:24
  • 1
    @nawfal: My suqestion is not how I can use `Directory.*`, but how I can write `IO.Directory.*` – Clément Feb 24 '12 at 15:33
  • You also need "using System;" along with "using System.IO;" - try that and it should work. – Matt T Feb 24 '12 at 15:36
  • 1
    In that case, the only workaround is what you have yourself posted in the question. That's the C# way of doing things. As to "why" it is so, I'm helpless, I was never bothered by it. – nawfal Feb 24 '12 at 15:37
  • As Matt mentioned you have to use fully qualified namespaces in C#. – nawfal Feb 24 '12 at 15:43
  • @MattT: No, adding `using System;` doesn't change anything. – Clément Feb 24 '12 at 15:52
  • I just put your code in my IDE. It's strange, it doesn't recognize IO as a valid namespace on my end either - this is possibly a bug in Visual Studio. Directory.GetFiles() does work - I suggest you use this. – Matt T Feb 24 '12 at 15:55
  • @MattT: Right, that's a solution: but I wanted to understand where this problem came from. – Clément Feb 24 '12 at 16:05
  • Hm... just hazarding a guess here, but it appears that the System.IO namespace is an alias. It may be a quirky bit of trivia in .NET. I'll research a bit more and see if I can find a more definitive answer. – Matt T Feb 24 '12 at 16:16
  • @MattT: Please do let me know if you find anything, at which point I'll accept your answer =) Thanks for your help! – Clément Feb 26 '12 at 14:38
0

The thing you are talking about is not posssible in C# that might be diffrence between C# and vb.NET.

If you are converting vb.Net code to C# than make use of this site will help you lot

vb.net to c#

Code fo ryou

System.IO.Directory.GetFiles(...)

or add

using System.IO;

will do for you

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263