none
DirectoryInfo and FileSystemInfo Asyn Method in Metro

    Question

  •  Hi, everyone, I have the following codes that want to transfer to a metro app.

    DirectoryInfo dir = new DirectoryInfo(_fromPath);

    FileSystemInfo[] FSinfo = dir.GetFileSystemInfos();

    private bool CopyFiles(FileSystemInfo[] FSInfo)

            {
                if (_stopNow)
                {
                    return true;
                }

                // Check the FSInfo parameter.
                if (FSInfo == null)
                {
                 
                    return false;
                }

                // Iterate through each item.


                foreach (FileSystemInfo i in FSInfo)
                {         //
                    try
                    {
                        // Check to see if this is a DirectoryInfo object.
                        if (i is DirectoryInfo)
                        {
                           
                            DirectoryInfo dInfo = (DirectoryInfo)i;

                            // Iterate (recurse) through all sub-directories.
                            CopyFiles(dInfo.GetFileSystemInfos());

                           
                        }
                        else if (i is FileInfo)   // Check to see if this is a FileInfo object.
                        {
                            
                           do sth;

    What makes me confused are the:

    DirectoryInfo dir = new DirectoryInfo(_fromPath);

    FileSystemInfo[] FSinfo = dir.GetFileSystemInfos();

    FileSystemInfo[] FSInfo,   

    FileSystemInfo i in FSInfo ,  

    i is DirectoryInfo  

    i is FileInfo  

    Can I still use the above code in Metro? And it seems that Asyn method is a better way. But I don't know the corresponed asyn method in Metro C#, can anybody help me?


    Friday, March 08, 2013 6:51 AM

Answers

  • You can process files and folders the following way.

    ----------------------------------------------------------------

    var YOUR_ROOT_FOLDER = ApplicationData.Current.LocalFolder.Path + "\\" + "YOUR_FOLDER_NAME";
            StorageFolder myFolder = await StorageFolder.GetFolderFromPathAsync(YOUR_ROOT_FOLDER);

                IReadOnlyList<StorageFile> fileList = await myFolder.GetFilesAsync();
                foreach (StorageFile file in fileList)
                {
                    // Process Files Here
                }

                IReadOnlyList<StorageFolder> folderList = await myFolder.GetFoldersAsync();
                foreach (StorageFolder folder in folderList)
                {
                    // Process Folders Here
                }

             ----------------------------------------------------------

    Alternatively, the you may process them using "IStorageItem" too.

      IReadOnlyList<IStorageItem> itemsList = await myFolder.GetItemsAsync();
                foreach (var item in itemsList)
                {
                    if (item is StorageFolder)
                    {
                        // Process Sub Folder Here
                    }
                    else
                    {
                        // Process File Here
                    }
                }

    Either way, put them in a method that can be called recursively and you can achieve what you are looking for. The libraries that may be useful in this context are Windows.Storage, Windows.Storage.Search, Windows.Storage.Streams etc.

    Hope this helps, let us know.

                            

    • Marked as answer by Delphi Huang Saturday, March 09, 2013 10:42 AM
    Saturday, March 09, 2013 6:36 AM

All replies

  • 天朝子民好多啊

    Friday, March 08, 2013 7:47 AM
  • This you have to use the StorageFile and StorageFolder. DirectoryInfo is not available

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, March 08, 2013 7:54 PM
  • Yes, you have to use a storage file.

    private async void GetFile(string filename)
    {
    StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(filename);
    }

    Use the import: "using Windows.Storage;"

    Saturday, March 09, 2013 5:31 AM
  • You can process files and folders the following way.

    ----------------------------------------------------------------

    var YOUR_ROOT_FOLDER = ApplicationData.Current.LocalFolder.Path + "\\" + "YOUR_FOLDER_NAME";
            StorageFolder myFolder = await StorageFolder.GetFolderFromPathAsync(YOUR_ROOT_FOLDER);

                IReadOnlyList<StorageFile> fileList = await myFolder.GetFilesAsync();
                foreach (StorageFile file in fileList)
                {
                    // Process Files Here
                }

                IReadOnlyList<StorageFolder> folderList = await myFolder.GetFoldersAsync();
                foreach (StorageFolder folder in folderList)
                {
                    // Process Folders Here
                }

             ----------------------------------------------------------

    Alternatively, the you may process them using "IStorageItem" too.

      IReadOnlyList<IStorageItem> itemsList = await myFolder.GetItemsAsync();
                foreach (var item in itemsList)
                {
                    if (item is StorageFolder)
                    {
                        // Process Sub Folder Here
                    }
                    else
                    {
                        // Process File Here
                    }
                }

    Either way, put them in a method that can be called recursively and you can achieve what you are looking for. The libraries that may be useful in this context are Windows.Storage, Windows.Storage.Search, Windows.Storage.Streams etc.

    Hope this helps, let us know.

                            

    • Marked as answer by Delphi Huang Saturday, March 09, 2013 10:42 AM
    Saturday, March 09, 2013 6:36 AM
  • Thank you, it works and it is fast
    Saturday, March 09, 2013 10:54 AM