2016-07-22 59 views
1

遞歸獲取文件,我想編寫一個程序來從蔚藍的文件存儲的文件,但問題是沒有定義的目錄深度和文件的isFile屬性始終返回false。目錄從蔚藍的文件存儲使用C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using Microsoft.Azure; // Namespace for Azure Configuration Manager 
using Microsoft.WindowsAzure.Storage; // Namespace for Storage Client Library 
using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage 
using Microsoft.WindowsAzure.Storage.File; // Namespace for File storage 

namespace AzureStorage 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); 

      //CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 
      // Create a CloudFileClient object for credentialed access to File storage. 
      CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); 

      // Get a reference to the file share we created previously. 
      CloudFileShare share = fileClient.GetShareReference("my-FileShare"); 

      // Ensure that the share exists. 
      if (share.Exists()) 
      { 
       // Get a reference to the root directory for the share. 
       CloudFileDirectory rootDir = share.GetRootDirectoryReference(); 


       // Get a reference to the directory we created previously. 
       CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("FILES"); 

       // Ensure that the directory exists. 
       if (sampleDir.Exists()) 
       { 
        var directoryLists = sampleDir.ListFilesAndDirectories(); 
        //sampleDir.ge 
        foreach (var yearDirTemp in directoryLists) 
        { 
         var yearDir = sampleDir.GetDirectoryReference(
             Path.GetFileNameWithoutExtension(yearDirTemp.Uri.LocalPath) 
             ); 
         foreach (var monthDirTemp in yearDir.ListFilesAndDirectories()) 
         { 
          var monthDir = yearDir.GetDirectoryReference(
              Path.GetFileNameWithoutExtension(monthDirTemp.Uri.LocalPath) 
              ); 
          foreach (var patientDirTemp in monthDir.ListFilesAndDirectories()) 
          { 
           var patientDir = monthDir.GetDirectoryReference(
                Path.GetFileNameWithoutExtension(patientDirTemp.Uri.LocalPath) 
                ); 
           foreach (var patientDataTemp in patientDir.ListFilesAndDirectories()) 
           { 
            var patientData = patientDir.GetDirectoryReference(
                Path.GetFileNameWithoutExtension(patientDataTemp.Uri.LocalPath) 
                ); 

            var fileList = patientData.ListFilesAndDirectories(); 
            foreach(var fileTemp in fileList) 
            { 
             // Here fileTemp could be file 
             // or directory containing more child directories 
             var file1 = patientData.GetFileReference(Path.GetFileName(fileTemp.Uri.LocalPath)); 
             file1.FetchAttributes(); 
             byte[] arrTarget = new byte[file1.Properties.Length]; 
             file1.DownloadToByteArray(arrTarget, 0);           
            } 

           } 
          } 

         }       
        } 
       } 
      } 
     } 
    } 
} 
+0

請分享您的代碼。 –

回答

1

深度沒有定義和文件的ISFILE屬性始終返回false。

如果您想在fileTemp爲File時執行一些操作,請使用以下代碼嘗試。我們可以使用GetType方法來獲取它的類型,然後使用name屬性來獲取它的類型值。如果該值爲「CloudFile」,那麼我們會執行一些操作,例如下載等。

foreach (var fileTemp in fileList) 
{ 
    // Here fileTemp could be file 
    // or directory containing more child directories 
    switch (fileTemp.GetType().Name) 
    { 
     case "CloudFile": 
      var file1 = patientData.GetFileReference(Path.GetFileName(fileTemp.Uri.LocalPath)); 
      file1.FetchAttributes(); 
      byte[] arrTarget = new byte[file1.Properties.Length]; 
      file1.DownloadToByteArray(arrTarget, 0); 
      break; 
     case "CloudFileDirectory": 
      break; 
    } 

} 
1
public static void list_file() 
    { 
     //***** Get list of all files/directories on the file share*****// 
     CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("xxxxxxxxxxxxxxxxxxxxxx_AzureStorageConnectionString")); 
     CloudFileClient fileClient = cloudStorageAccount.CreateCloudFileClient(); 
     CloudFileShare fileShare = fileClient.GetShareReference("dummyfile"); 

     // List all files/directories under the root directory. 
     Console.WriteLine("Getting list of all files/directories under the root directory of the share."); 

     IEnumerable<IListFileItem> fileList = fileShare.GetRootDirectoryReference().ListFilesAndDirectories(); 

     // Print all files/directories listed above. 
     foreach (IListFileItem listItem in fileList) 
     { 
      // listItem type will be CloudFile or CloudFileDirectory. 
      Console.WriteLine(listItem.Uri.Segments.Last()); 
      Console.WriteLine(listItem.GetType()); 
      if(listItem.GetType()== typeof(Microsoft.WindowsAzure.Storage.File.CloudFileDirectory)) 
      { 

       list_subdir(listItem); 
      } 
     } 
    } 
    public static void list_subdir(IListFileItem list) 
    { 
     Console.WriteLine("subdir"); 
     CloudFileDirectory fileDirectory=(CloudFileDirectory)list; 
     IEnumerable<IListFileItem> fileList = fileDirectory.ListFilesAndDirectories(); 


     // Print all files/directories in the folder. 
     foreach (IListFileItem listItem in fileList) 
     { 
      // listItem type will be CloudFile or CloudFileDirectory. 
      Console.WriteLine(listItem.Uri.Segments.Last()); 
     } 
    } 
+0

在這個程序中,顯示目錄及其相應的子文件夾。 –

+0

執行上述程序的步驟是:獲取共享根目錄下的所有文件/目錄列表。 DIR Microsoft.WindowsAzure.Storage.File.CloudFileDirectory 子目錄 文件%201.txt Microsoft.WindowsAzure.Storage.File.CloudFile MYDIR Microsoft.WindowsAzure.Storage.File.CloudFileDirectory 子目錄 note.txt 新聞任何關鍵要繼續。 。 。 –

+0

的輸出是: [1]:https://i.stack.imgur.com/eNEq4.png –