locked
How to check if the folder exist or not ! RRS feed

  • Question

  • How to check if the folder exist or not !
    Saturday, September 29, 2012 7:04 PM

Answers

  • Try to open it. If it succeeds then it exists. If it doesn't, then catch and handle the exception.

    If you  are going to create it if it doesn't exist then just create it and set the CreationCollisionOption to OpenIfExists.

    --Rob

    Saturday, September 29, 2012 7:13 PM
    Moderator
  • use this:

    Windows.Storage.StorageFolder folder = await picker.PickSingleFolderAsync();
    
    StorageFolder
    
    
    
    
    targetFolder = await folder.CreateFolderAsync("Documents", CreationCollisionOption.OpenIfExists)
               
              
                var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".exe" });
                var query = folder.CreateFileQueryWithOptions(queryOptions);
                var files = await query.GetFilesAsync();
    
    
                foreach (var file in files)
                {
                    await file.MoveAsync(targetFolder);
                } 
    

    it creates if not exists, else it opens it

    • Marked as answer by Hesham Nouby Saturday, September 29, 2012 9:53 PM
    Saturday, September 29, 2012 8:52 PM

All replies

  • Try to open it. If it succeeds then it exists. If it doesn't, then catch and handle the exception.

    If you  are going to create it if it doesn't exist then just create it and set the CreationCollisionOption to OpenIfExists.

    --Rob

    Saturday, September 29, 2012 7:13 PM
    Moderator
  • that's right or ?
     Windows.Storage.StorageFolder folder = await picker.PickSingleFolderAsync();
    
    
                 
               try
                {
                 var  targetFolder = await folder.GetFolderAsync("Documents");
                }
                catch
                {  
                    folder.CreateFolderAsync  ("documents");
    
                }
               
              
                var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".exe" });
                var query = folder.CreateFileQueryWithOptions(queryOptions);
                var files = await query.GetFilesAsync();
    
    
                foreach (var file in files)
                {
                    await file.MoveAsync(targetFolder);
                } 

    there r an error in targetfolder cause it is in try-cach
    Saturday, September 29, 2012 7:30 PM
  • Can u help me plz :)
    Saturday, September 29, 2012 8:10 PM
  • use this:

    Windows.Storage.StorageFolder folder = await picker.PickSingleFolderAsync();
    
    StorageFolder
    
    
    
    
    targetFolder = await folder.CreateFolderAsync("Documents", CreationCollisionOption.OpenIfExists)
               
              
                var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".exe" });
                var query = folder.CreateFileQueryWithOptions(queryOptions);
                var files = await query.GetFilesAsync();
    
    
                foreach (var file in files)
                {
                    await file.MoveAsync(targetFolder);
                } 
    

    it creates if not exists, else it opens it

    • Marked as answer by Hesham Nouby Saturday, September 29, 2012 9:53 PM
    Saturday, September 29, 2012 8:52 PM
  •  folder.CreateFolderAsync  ("documents").GetResult();

    • Edited by asifmustak Thursday, September 4, 2014 3:01 AM
    Thursday, September 4, 2014 3:01 AM
  • Rob 

    if it is async

    you should use GetResult at end

    like 

     folder.CreateFolderAsync  ("documents").GetResult();


    Thursday, September 4, 2014 3:04 AM
  • Please don't bump very old posts. If you have a new question then please post it in a new thread.

    You should not typically call GetResult on async functions. In almost all cases (and in particular in the cases discussed in this thread) you should await the method.

    --Rob

    Thursday, September 4, 2014 6:21 AM
    Moderator