如何以编程方式区分Azure文件共享中的目录和文件?

时间:2018-07-20 16:08:01

标签: c# azure azure-storage-blobs azure-files

我有一段类似if的代码

$thumbnailPath
我发现的

是错误的,因为即使 // last path segement can be like "binaries.x86" since // directories can have `.` characters in them if ((lastItemInPath = cloudFileDirectory.GetFileReference(lastPathSegment)) != null) { // the last path segment is a file return lastItemInPath; } else if ((lastItemInPath = cloudFileDirectory.GetDirectoryReference(lastPathSegment)) != null) { // the last path segment is a directory return lastItemInPath; } 确实是目录,第一个if仍返回true。知道我该如何准确区分吗?

1 个答案:

答案 0 :(得分:0)

Try this 

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Azure Storage Connection String")); 
CloudFileClient fileClient = cloudStorageAccount.CreateCloudFileClient(); 
CloudFileShare fileShare = fileClient.GetShareReference("File Share Name"); 

 IEnumerable<IListFileItem> fileList = fileShare.GetRootDirectoryReference().ListFilesAndDirectories(); 
 foreach (var fileTemp in fileList)  
 { 
    switch (fileTemp.GetType().Name) 
     { 
         case "CloudFile": 
             //Write your logic here 
             break; 
         case "CloudFileDirectory": 
             //Write your logic here 
             break; 
     }
 }