Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

.net - 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:Windowszzzz"
    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:UsersmyUserAppDataLocalVirtualStoreWindows 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...