tags:

views:

89

answers:

2

I'm getting a "DirectoryNotFoundException" error, here is the code:

string directorio = "D:\MUSICA\La Trampa - El Mísero Espiral De Encanto";
DirectoryInfo dir = new DirectoryInfo(directorio);
DirectoryInfo[] dirs = dir.GetDirectories(); <------------This is the line I'm having this problem.

I believe it's caused when it tries to parse the tilde part of that string Mísero. the directory D:\MUSICA\La Trampa - El Mísero Espiral De Encanto exists because I can see it and also have some files in it. Is there any way to send this string in correct way?

Thanks

+7  A: 

Your code would not work to begin with, as you have illegal escape codes (\M and \L) in the string.

You need to escape you backslashes, or use a string literal:

string directorio = @"D:\MUSICA\La Trampa - El Mísero Espiral De Encanto";

Or:

string directorio = "D:\\MUSICA\\La Trampa - El Mísero Espiral De Encanto";

Otherwise the M from MUSICA is escaped as is the L from La. As I mentioned already these are not legal escape codes, as can be seen here.

Oded
I'm not sure if that is the case, I can access to other directories inside D:\MusicaThanks anyway
HoNgOuRu
@HoNgOuRu - read this, perhaps this will convince you... http://msdn.microsoft.com/en-us/library/ms228362.aspx
Oded
-1. "Unrecognized escape sequence" is the error you get with that string. He might have shown "wrong" code, but he most certainly didn't gave the right code - it wouldn't even compile.
Benjamin Podszun
@Benjamin Podszun - Thanks for pointing this out. I have updated my answer to reflect the issue with the OP code.
Oded
Fair enough, removed the downvote. But - it somehow is of dubious value for the topic at hand.. ;-) The OP didn't give the right code for the (unrelated) exception and "solved" it as a Filesystem error.
Benjamin Podszun
A: 

Thanks for your answer, The problem was from other kind. The OS cannot delete that directory either, it says that the directory is not there while it is. This could be caused by a HD error. I'll try to fix it, but nothing has to do with my question. Thanks again.

HoNgOuRu