Why can't I write IO.Directory.GetFiles?
Asked Answered
L

2

8

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 ??"

Littles answered 24/2, 2012 at 15:9 Comment(1)
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.Wyckoff
F
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.)

Fenugreek answered 24/2, 2012 at 15:11 Comment(14)
+1 Nice, just of interest perhaps you have any references to MSDN regarding such requirement it would be greatEonian
This yields The name 'IO' does not exist in the current context when I write IO.Directory.GetFilesOjeda
@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.IOEonian
@Littles , add using System.IO; to your namespace collection at the top after which you can simply write Directory.GetFiles(...)Wyckoff
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.Fenugreek
@nawfal: My suqestion is not how I can use Directory.*, but how I can write IO.Directory.*Ojeda
You also need "using System;" along with "using System.IO;" - try that and it should work.Fenugreek
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.Wyckoff
As Matt mentioned you have to use fully qualified namespaces in C#.Wyckoff
@MattT: No, adding using System; doesn't change anything.Ojeda
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.Fenugreek
@MattT: Right, that's a solution: but I wanted to understand where this problem came from.Ojeda
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.Fenugreek
@MattT: Please do let me know if you find anything, at which point I'll accept your answer =) Thanks for your help!Ojeda
H
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

Haughay answered 24/2, 2012 at 15:12 Comment(2)
I'd like to get rid of the System prefix.Ojeda
@Littles , add using System.IO; to your namespace collection at the top!Wyckoff

© 2022 - 2024 — McMap. All rights reserved.