为什么IO.Directory.CreateDirectory成功当它不应该?不应、当它、IO、Directory

2023-09-03 00:09:53 作者:囄wǒ遠点

我在Vista旗舰版中运行Visual Studio 2008。当我创建一个全新的控制台应用程序,并通过调试器中运行以下code,我得到预期的结果 - 一个 UnauthorizedAccessException 被抛出,而不是创建目录。

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

当我运行code同样位从我的生产解决方案,在 IO.Directory.CreateDirectory()方法的没有的抛出一个异常, IO.Directory.Exists()返回True,和目录实际上并没有得到在磁盘上创建的。

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.

是否有任何项目/解决方案的设置,将使IO.Directory.CreateDirectory()的行为改变这样?

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

注意:我的没有的运行Visual Studio在这两种情况下,管理员

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

编辑:生产应用程序在同一个盒子作为测试应用程序中运行

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

编辑#2:生产应用程序正在使用虚拟化技术。我点击我的电脑,浏览到C:\ Windows和浏览器工具栏上点击兼容性文件,它给我带来了C:\用户\ MYUSER \应用程序数据\本地\ VirtualStore \ Windows其中我创建的目录坐着。

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

答:控制台应用程序,默认情况下,用一个app.manifest创建。生产中的应用 - 这是一个WPF应用程序 - 没有一个app.manifest。显然,Vista将使用虚拟化,如果没有app.manifest present的execuable。

谢谢大家!

推荐答案

Windows Vista的是让你创建的目录,但它被存储在别处。只有创建它可以在你指定的路径查看应用程序。这就是为什么存在返回true。

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.

这是摆在使旧的应用程序试图将文件保存在文件夹那里有没有权限也不会失败。大多数人都遇到了这个与尝试保存在Program Files目录中的文件遗留应用程序。

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).

这也影响到注册表中。

您可以阅读更多关于它的here.

You can read more about it here.

下面是Microsoft的参考。

Here is a reference from Microsoft.