0
var appFolder = await KnownFolders.DocumentsLibrary.CreateFolderAsync("Test");

Update: Got it - Used Above Line.------

var folder = KnownFolders.DocumentsLibrary;
StorageFolder subFolder = await folder.GetFolderAsync("Test");
List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".txt");
QueryOptions queryOptions = new QueryOptions(Windows.Storage.Search.CommonFileQuery.OrderByName, fileTypeFilter);
StorageFileQueryResult queryResult = subFolder.CreateFileQueryWithOptions(queryOptions);
var files = await queryResult.GetFilesAsync();

foreach (var file in files)
{
   string name = file.Name;
   Debug.WriteLine(name);
}

Learning to build for UWP. Here is the code to get all the text files from a folder in the Documents folder. But I want to create the Test folder if it doesn't exist in the documents folder.

Ajay G
  • 45
  • 8
  • Does this answer your question? [If a folder does not exist, create it](https://stackoverflow.com/questions/9065598/if-a-folder-does-not-exist-create-it) – User2585 Jan 30 '20 at 15:07
  • That answer isn't for UWP. – Ajay G Jan 30 '20 at 15:11
  • You can still use the same code, it's a C#/ .net question not specific for UWP – User2585 Jan 30 '20 at 15:19
  • Getting Error : System.UnauthorizedAccessException: 'Access to the path 'C:\Users\....\bin\x86\Debug\AppX\Typing' is denied.' – Ajay G Jan 30 '20 at 15:41
  • @User2585 UWP has a different security model from Desktop. – Raymond Chen Jan 30 '20 at 17:49
  • Note that your question doesn't match your title. Your title asks about checking if a folder exists, but your question is about creating a folder if it doesn't already exist. The way to check if something exists is to call `TryGetItemAsync` and see if it returns something. If you want to create a folder if it doesn't already exist then use `CreateFolderAsync` with a collision option of `OpenIfExists` to say "If it already exists, then just give me the existing item." – Raymond Chen Jan 30 '20 at 17:50

2 Answers2

0
if(!System.IO.Directory.Exists(System.IO.Path.Combine(folder,"Test")))
{
    System.IO.Directory.Create(System.IO.Path.Combine(folder,"Test")));
} 

Might have the syntax slighly out as not doing it on a computer, but I think that should do it.

Bigtoe
  • 3,372
  • 1
  • 31
  • 47
0

Try to use CreationCollisionOption!

var appFolder = await KnownFolders.DocumentsLibrary
               .CreateFolderAsync("Test",  CreationCollisionOption.OpenIfExists);
//CreationCollisionOption have : 
//                              1 (OpenIfExists) 
//                              2 (GenerateUniqueName) 
//                              3 (ReplaceExisting)
//                              4 (FailIfExists)

Now If you need to create a Folder and check if existed in different place, you need a Folder to put in it.
So for sure you need a directory at least!
Therefor if the folder do not exist, you can then take the directory and loop throw the path until you rich where it miss, you can't be sure if any root folders are missing.

public static class Ex_storegeFolder
    {
        public static async Task<StorageFolder> CreateFolderIfNotExist( this string path )
        {
            StorageFolder xs = null;

            string root = path.Substring( 0 , 3 );
            string xpath = path.Substring( 4 );
            
            try
            {
                xs = await StorageFolder.GetFolderFromPathAsync( path );
            }
            catch
            {
                xs = await StorageFolder.GetFolderFromPathAsync( root );
                string[] array = xpath.Split( '\\' );
                for ( int i = 0 ; i < array.Length ; i++ )
                {
                    xs = await xs.CreateFolderAsync( array[i] , CreationCollisionOption.OpenIfExists );

                }

            }
            return xs;
        }
    }

You can call it like this:

string path = @"C:\users\User1\oo\d"
StorageFolder folder = await path.CreateFolderIfNotExist();
rynex akil
  • 15
  • 1
  • 8