为什么 IO.Directory.CreateDirectory 不应该成功? [英] Why is IO.Directory.CreateDirectory succeeding when it shouldn't?

查看:15
本文介绍了为什么 IO.Directory.CreateDirectory 不应该成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Vista Ultimate 机器上运行 Visual Studio 2008.当我创建一个全新的控制台应用程序并通过调试器运行以下代码时,我得到了预期的结果 - 抛出 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: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

当我从我的生产解决方案中运行同样的代码时,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:UsersmyUserAppDataLocalVirtualStoreWindows,我创建的目录所在的位置.

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

答案:默认情况下,控制台应用程序是使用 app.manifest 创建的.生产应用程序(WPF 应用程序)没有 app.manifest.显然,如果可执行文件没有 app.manifest,Vista 将使用虚拟化.

谢谢大家!

推荐答案

Windows Vista 允许您创建目录,但它会将它存储在其他地方.只有创建它的应用程序才能在您指定的路径中看到.这就是 Exists 返回 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.

这样做是为了让旧应用程序尝试将文件保存在没有权限的文件夹中不会失败.大多数人在尝试将文件保存在程序文件目录中的旧应用中都会遇到这种情况.

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

这也会影响注册表.

您可以在这里阅读更多相关信息.

这是来自 Microsoft 的参考.

Here is a reference from Microsoft.

这篇关于为什么 IO.Directory.CreateDirectory 不应该成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆