我的程序需要将内容存储在“%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'.
我就是不明白。也许与虚拟化有关?
发生了什么事以及如何解决?
最佳答案
只有两个小提示:
-
Directory.CreateDirectory
已经递归地工作了。不需要您的Creator
类。我建议您将其删除。 - 您的组合路径将为 %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/