Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is IO.Directory.CreateDirectory succeeding when it shouldn't?

I'm running Visual Studio 2008 on a Vista Ultimate box. When I create a brand new console application and run the following code via the debugger, I get an expected result -- an UnauthorizedAccessException is thrown and the directory is not created.

Sub Main()
    Dim path = "C:\Windows\zzzz"
    Try
        IO.Directory.CreateDirectory(path)
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
    Console.WriteLine(IO.Directory.Exists(path).ToString)
    Console.ReadLine()
End Sub

When I run this same bit of code from my production solution, the IO.Directory.CreateDirectory() method is not throwing an exception, IO.Directory.Exists() returns True, and the directory does not actually get created on disk.

Are there any project/solution settings that would make the behavior of IO.Directory.CreateDirectory() vary like this?

Note: I am not running Visual Studio as an administrator in either case.

EDIT: The production application is running on the same box as the test application.

EDIT #2: The production application is using virtualization. I clicked on My Computer, navigated to C:\Windows, and clicked on "Compatibility Files" on the explorer toolbar and it brought me to C:\Users\myUser\AppData\Local\VirtualStore\Windows where my created directories were sitting.

The only outstanding question is - why does the production application virtualize while the test console application throws an exception??

Answer: the console application, by default, was created with an app.manifest. The production application--which is a WPF application--did not have an app.manifest. Apparently Vista will use virtualization if there is no app.manifest present for the execuable.

Thanks everyone!

like image 652
Rob Sobers Avatar asked May 20 '09 14:05

Rob Sobers


1 Answers

Windows Vista is letting you create the directory but it is storing it somewhere else. Only the application that created it can see in the path you specified. That is why Exists returns true.

This was put in so that old applications trying to save files in folders where there were no permissions wouldn't fail. Most people run into this with legacy apps that try to save their files in the program files directory.

This is called virtualization and you can set a manifest saying that you don't want it for your application. Also if you run with elevated privileges it doesn't apply (which is not your case).

This also affects the registry.

You can read more about it here.

Here is a reference from Microsoft.

like image 137
jvanderh Avatar answered Oct 31 '22 03:10

jvanderh