c# - 代码在本地 Windows 2008 上运行正常,但无法在 Azure 云上的应用程序数据中创建子文件夹

标签 c# .net azure cloud virtualization

我的程序需要将内容存储在“%AllUsersProfile%\Application Data\Stuff”中(取决于我无法更改的代码)。我使用以下代码递归创建文件夹:

class DirCreator {
   internal static void Create( String path )
   {
       create( new DirectoryInfo( path ) );
   }
   void create( DirectoryInfo info )
   {
       String path = info.FullName;
       if( Directory.Exists( path ) ) {
           return;
       }
       if( info.Parent != null ) {
           create( info.Parent );
       }
       Directory.CreateDirectory( path );
   }

}

调用者代码执行以下操作:

String appData = Environment.GetFolderPath(
    Environment.SpecialFolder.CommonApplicationData );
Creator.Create( Path.Combine( appData, "Application Data\\Stuff" ) );

我经常使用该代码,因为我发现简单的 Directory.CreateDirectory() 不起作用。

现在在我的本地 Windows 2008 R2 Standard 64 位上,上面的代码可以正常工作。当我在 Azure 角色 OnStart() 中执行相同操作时,会发生以下情况:

  • Environment.GetFolderPath() 返回 D:\ProgramData
  • 一旦递归下降并调用 Directory.Exists( "D:\\ProgramData\\ApplicationData"),该调用将返回 true 并且递归结束
  • 稍后 Directory.CreateDirectory( "D:\\ProgramData\\Application Data\\Stuff") 调用失败,并显示

Could not find a part of the path 'D:\ProgramData\Application Data\Stuff'.

我就是不明白。也许与虚拟化有关?

发生了什么事以及如何解决?

最佳答案

只有两个小提示:

  1. Directory.CreateDirectory已经递归地工作了。不需要您的 Creator 类。我建议您将其删除。
  2. 您的组合路径将为 %AllUsersProfile%\Application Data\Application Data\Stuff。注意到双重应用程序数据了吗? Environment.SpecialFolder.CommonApplicationData 返回 %AllUsersProfile%\Application Data,您可以将其与 Application Data\Stuff...

关于c# - 代码在本地 Windows 2008 上运行正常,但无法在 Azure 云上的应用程序数据中创建子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6691517/

相关文章:

c# - 如何验证 XPS 文档?

.net - C++/CLI中的变量参数列表

c# - Azure 服务总线主题架构

c# - 从 C# 代码生成包含触发器的 WPF 样式

c# - 使用 CommandLine Parser Library 标记所需集

c# - 在预定义的时间高效运行数十万个函数

azure - 在数据工厂中映射数据流时出现无转义字符错误

azure - 尝试 psping 到 azure 计算机防火墙已关闭,但仍然无法连接

c# - LDAP 查询中的 LDAP 注入(inject) c#

c# - 亚马逊 s3 : direct upload vs presigned url