tags:

views:

408

answers:

3

I am working on an application for Windows 7, and run some routine directory creation code:

string dirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyDir");    
if (!Directory.Exists(dirPath))
   Directory.CreateDirectory(dirPath);

The problem is that the Directory.Exists line returns true, when I can't see the directory through command line and Windows Explorer. This isn't an issue when working with Windows XP. Is there something going on with Windows 7 that I'm not aware of?

EDIT: Added Path.Combine

A: 

I don't know why Windows 7 is doing that, and I don't have a copy to test, but your check to Directory.Exists(path) shouldn't be necessary. If you reflect (deep) into Directory.CreateDirectory(path), you'll find that it internally checks to see if the directory already exists, and it's not a problem to call it multiple times on a directory that already exists. The call to Directory.Exists(path) is extraneous and unnecessary.

Of course, if Windows 7 isn't doing the Directory.Exists the way I'd expect, maybe it doesn't do the naked Directory.CreateDirectory either. In any case, it's worth testing.

David
David - I wondered about that on Directory.CreateDirectory. However, when running Directory.CreateDirectory with the same folder path the directory isn't created either. I realize this is a hard question to answer without being able to mimic the environment, so I may have to continue to do more research.
Blake Blackwell
A: 

CommonApplicationData typically resolves to <OSDrive>\ProgramData on Windows 7. This is a hidden folder. If you don't ask Explorer to show hidden files and folders (from the Folder Options->View UI), you won't see it in Explorer.

EDIT: Make sure you're viewing the correct directory in Explorer: browse to %PROGRAMDATA%, not C:\ProgramData.

Michael Petrotta
Hi Michael - I can see C:\ProgramData but I can't see my directory underneath it. I'm running as administrator, if that helps any.
Blake Blackwell
Hmmm. In that case, I don't know. Your code works fine for me. I suggest, given that your copied code was incorrect before your edit, that you try it again, copying the code directly from your question.
Michael Petrotta
Would it possibly have anything to do with the code running on the VM? I'd post the original code, but it uses another class so it will be difficult to piece together.
Blake Blackwell
I wouldn't think so.
Michael Petrotta
+2  A: 

Please understand that Windows Vista and Windows 7 use virtualization to protect such folders, so you need to check if myDir is in virtualstore,

C:\Users(user name)\AppData\Local\VirtualStore\ProgramData

Lex Li
To expand on this: if you previously tried to create MyDir from a non-elevated process, Windows set up a virtualized copy for you. I believe you'll continue to see this virtualized copy even if you check Directory.Exists from an elevated process.
Richard Berg
I think this is closer to the answer I need, but I checked in VirtualStore and didn't see MyDir there. Do you have some articles about elevated processes? I'm not familiar with this concept.
Blake Blackwell
Besides, you can utilize Process Monitor to check actually which folder your application was trying to access to understand where the MyDir is. (http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) Play with it and you will find it wonderful.
Lex Li
You can better understand UAC for Windows Vista and Windows 7 from MSDN. http://msdn.microsoft.com/en-us/library/bb756960.aspx Most people get confused about virtualization there, but once you are familiar with it, you can fight against it easily :)
Lex Li